
> sink(con, append=TRUE, type="message")

> tic() # time running all of the code

> # print current version of R and print current date and time
> getRversion()
[1] ‘4.2.1’

> print(Sys.time())
[1] "2024-02-12 00:09:32 EST"

> # create directories to store output
> if(!dir.exists('results')){
+   dir.create("results")
+ }

> if(!dir.exists('results/sds')){
+   dir.create("results/sds")
+ }

> if(!dir.exists('figures')){
+   dir.create("figures")
+ }

> if(!dir.exists('tables')){
+   dir.create("tables")
+ }

> # Manuscript Figures and Tables
> 
> 
> # Figure 1 -- No code for figure 1 in repository. Maps require exact residential address information which i .... [TRUNCATED] 

> source('FIG-party-switching-matrix.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
          used (Mb) gc trigger (Mb) max used (Mb)
Ncells  635959 34.0    1217080   65  1217080 65.0
Vcells 1746509 13.4    8388608   64  2007024 15.4

> library(data.table)
data.table 1.14.2 using 1 threads (see ?getDTthreads).  Latest news: r-datatable.com

Attaching package: ‘data.table’

The following object is masked from ‘package:tictoc’:

    shift


> library(ggplot2)

> library(scales)

> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:data.table’:

    between, first, last

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union


> library(stringr)

> source('theme_jake.R')
Registering fonts with R

> ### Figure 2 -- PARTISAN TRANSITION MATRICES
> 
> # Here, we output the transition matrices figures detailing the number and percentage,
> # of voters of each year1 party that are of each party in year2, for each linked sample.
> 
> 
> 
> # Load in analysis data
> data = rbindlist(lapply(c('panel-analysis-2008-2012.csv.gz',
+                           'panel-analysis-2012-2016.csv.gz',
+                           'panel-analysis-2016-2020.csv.gz'), FUN = function(x){
+                             d=fread(x, select = c('Party_year1','DemDiff','RepDiff'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+                             
+                             
+                             return(d)
+                           }))
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> gc()
            used   (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells   1036639   55.4    2115057  113.0   1217080   65.0
Vcells 235903162 1799.8  451455675 3444.4 443771446 3385.8

> # Set party codes for year1 and year2
> data[!Party_year1%in%c('Democrat','Republican'),Party_year1:='Non-Partisan']

> data[DemDiff==1|(Party_year1=='Democrat' & DemDiff==0),Party_year2:='Democrat']

> data[RepDiff==1|(Party_year1=='Republican' & RepDiff==0),Party_year2:='Republican']

> data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']

> data[,DemDiff:=NULL]

> data[,RepDiff:=NULL]

> # Make year1 and year2 variables
> data[,Year1:=substr(Years,1,4)]

> data[,Year2:=substr(Years,6,9)]

> # Create 2016-2020 matrix
> d = data[Years=='2016-2020']

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Party_year1:=paste0('2016\n',Party_year1)]

> p[,Party_year2:=paste0('2020\n',Party_year2)]

> g1=ggplot(p, aes(y = factor(Party_year1), x = factor(Party_year2), fill = color, label =prob))+
+   geom_tile(aes(alpha=a))+
+   scale_fill_manual(values=c('red','blue', 'grey','pink','lightblue','purple'))+
+   scale_alpha_manual(values = c(.5,.2,.1))+
+   geom_text( size = 8, color = 'black') +
+   geom_text(aes(label = paste0('(n = ',comma(Total),')')),nudge_y=-.15, size = 5, color = 'black') +
+   #scale_x_discrete(position = "top") +
+   coord_equal() +
+   geom_vline(xintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   geom_hline(yintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   ylab("") +  
+   coord_equal() +
+   xlab(paste(''))+
+   ylab(paste(''))+
+   theme_jake_tile2()+
+   theme(axis.text.y=element_text(angle=90,hjust=.5), text = element_text(size=24))
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

> # store output
> p1620=p

> ## Make 2012-2016 matrix
> 
> d = data[Years=='2012-2016']

> gc()
            used   (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells   1151677   61.6    2115057  113.0   2092034  111.8
Vcells 487545950 3719.7  780406606 5954.1 749186571 5715.9

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Party_year1:=paste0('2012\n',Party_year1)]

> p[,Party_year2:=paste0('2016\n',Party_year2)]

> g2=ggplot(p, aes(y = factor(Party_year1), x = factor(Party_year2), fill = color, label =prob))+
+   geom_tile(aes(alpha=a))+
+   scale_fill_manual(values=c('red','blue', 'grey','pink','lightblue','purple'))+
+   scale_alpha_manual(values = c(.5,.2,.1))+
+   geom_text( size = 8, color = 'black') +
+   geom_text(aes(label = paste0('(n = ',comma(Total),')')),nudge_y=-.15, size = 5, color = 'black') +
+   #scale_x_discrete(position = "top") +
+   coord_equal() +
+   geom_vline(xintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   geom_hline(yintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   ylab("") +  
+   coord_equal() +
+   xlab(paste(''))+
+   ylab(paste(''))+
+   theme_jake_tile2()+
+   theme(axis.text.y=element_text(angle=90,hjust=.5), text = element_text(size=24))
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

> # store output
> p1216=p

> ## Make 2008 - 2012 matrix
> d = data[Years=='2008-2012']

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Party_year1:=paste0('2008\n',Party_year1)]

> p[,Party_year2:=paste0('2012\n',Party_year2)]

> g3=ggplot(p, aes(y = factor(Party_year1), x = factor(Party_year2), fill = color, label =prob))+
+   geom_tile(aes(alpha=a))+
+   scale_fill_manual(values=c('red','blue', 'grey','pink','lightblue','purple'))+
+   scale_alpha_manual(values = c(.5,.2,.1))+
+   geom_text( size = 8, color = 'black') +
+   geom_text(aes(label = paste0('(n = ',comma(Total),')')),nudge_y=-.15, size = 5, color = 'black') +
+   #scale_x_discrete(position = "top") +
+   coord_equal() +
+   geom_vline(xintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   geom_hline(yintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   ylab("") +  
+   coord_equal() +
+   xlab(paste(''))+
+   ylab(paste(''))+
+   theme_jake_tile2()+
+   theme(axis.text.y=element_text(angle=90,hjust=.5), text = element_text(size=24))
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

> library(cowplot)

> ggsave(plot = plot_grid(g3,g2,g1,nrow=1, labels = c('2008-2012', '2012-2016', '2016-2020'), hjust=-3.1), file = paste0('figures/Fig2.png'),width=18,height=6)

> rm(list=ls())

> gc()
           used  (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells  1258582  67.3    2115057  113.0   2115057  113.0
Vcells 28487607 217.4  624325285 4763.3 749186571 5715.9

> toc()
840.716 sec elapsed

> gc()
           used  (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells  1258892  67.3    2115057  113.0   2115057  113.0
Vcells 28482097 217.4  499460228 3810.6 749186571 5715.9

> # Figure 3
> tic() # time scripts for each figure

> source('FIG-distributions.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
           used  (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells  1259857  67.3    2115057  113.0   2115057  113.0
Vcells 28490715 217.4  399568183 3048.5 749186571 5715.9

> library(ggplot2)

> library(scales)

> library(data.table)

> library(stringr)

> library(dplyr)

> library(tibble)

> library(glue)

> library(scales)

> library(tidyr)

> library(cowplot)

> ### Figure 3 - SEGREGATION CHANGES DISTRIBUTION
> 
> # Here, we want to output graphics that plot the distribution of year over year changes in segregation for each linked analysis dataset.
> 
> 
> 
> 
> 
> 
> # Load files 
> data = rbindlist(lapply(c('panel-analysis-2008-2012.csv.gz',
+                           'panel-analysis-2012-2016.csv.gz',
+                           'panel-analysis-2016-2020.csv.gz'), FUN = function(x){
+   d=fread(x, select = c('DemSpExpDiff_nohh','RepSpExpDiff_nohh'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+                           }))
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> ## Make Plots 
> 
> holder1 = data[,c('DemSpExpDiff_nohh','Years')]

> holder1[,Exposure:=DemSpExpDiff_nohh][,Exposure.Type:='Exposure\nto Democrats']

> holder1= holder1[,c('Exposure','Exposure.Type', 'Years')]

> holder2 = data[,c('RepSpExpDiff_nohh','Years')]

> holder2[,Exposure:=RepSpExpDiff_nohh][,Exposure.Type:='Exposure\nto Republicans']

> holder2= holder2[,c('Exposure','Exposure.Type', 'Years')]

> holder = rbind(holder1,holder2)

> rm(holder1,holder2)

> gc()
            used   (Mb) gc trigger (Mb)   max used   (Mb)
Ncells   1272249   68.0    2115057  113    2115057  113.0
Vcells 677725021 5170.7 1178062661 8988 1093429067 8342.3

> colors = c(`Exposure\nto Democrats` = "#377EB8",`Exposure\nto Republicans` = "#E41A1C")

> g=  ggplot(data=holder, aes(x=Exposure,color=Exposure.Type, linetype=Years))+
+   geom_density()+
+   theme_bw()+
+   facet_grid(rows = vars(Exposure.Type))+
+   scale_linetype_manual(values=c('dotted','dashed', 'solid'))+
+   theme(legend.position='bottom', legend.title=element_blank(), text=element_text(size=18))+
+   scale_color_manual(values=colors) +
+   xlim(-.5,.5)+
+   theme(text=element_text(family='serif', size = 24))+
+   xlab('')+
+   ylab('Density')+
+   guides(color='none')

> names(data) = c( 'Exposure\nto Democrats', 'Exposure\nto Republicans', 'Years')

> ###
> 
> 
> # get values for central tendencies
> d = holder[,list(Mean=mean(Exposure,na.rm=T), Median=median(Exposure,na.rm=T),SD=sd(Exposure,na.rm=T)),by=c('Years','Exposure.Type')]%>%
+   as_tibble()%>%
+   pivot_longer(Mean:SD)

> d = d %>% mutate(Exposure.Type=factor(Exposure.Type, levels = c('Exposure\nto Republicans','Exposure\nto Democrats')))

> # theme for central tendency part of figure
> theme_jake_tile2 <- function(base_size = 10, base_family = "serif",
+                              base_line_size = base_size / 22,
+                              base_rect_size = base_size / 22) {
+   # Starts with theme_bw and remove most parts
+   theme_bw(
+     base_size = base_size,
+     base_family = base_family,
+     base_line_size = base_line_size,
+     base_rect_size = base_rect_size
+   ) %+replace%
+     theme(
+       plot.title = element_text(size = 15),
+       panel.grid.major = element_blank(), 
+       panel.grid.minor = element_blank(),
+       axis.ticks      = element_blank(),
+       legend.background = element_blank(),
+       legend.key        = element_blank(),
+       panel.background  = element_blank(),
+       panel.border      = element_blank(),
+       strip.background  = element_blank(),
+       plot.background   = element_blank(),
+       legend.position   = 'none',
+       # axis.title.x=element_blank()
+       
+       complete = TRUE
+     )
+ }

> d$y=1

> d$value=round(d$value, digits = 2)

> d$value=ifelse(d$value==0,0,d$value)

> t=ggplot(d,aes(y=Exposure.Type, x = name,fill=Exposure.Type,  label = sprintf("%0.2f", value)))+
+   geom_tile(alpha=.75)+
+   scale_fill_manual(values=colors)+
+   geom_text( size = 5, alpha =1, color='black') +
+   scale_x_discrete(position = "top") +
+   scale_y_discrete(position = "right") +
+   geom_vline(xintercept = 1:8 - 0.5, colour = "white", size = 1) +
+   geom_hline(yintercept = 1:8 - 0.5, colour = "white", size = 1) +
+   ylab("") +  
+   xlab('') +
+   coord_equal() +
+   theme_jake_tile2()+
+   theme(text=element_text(size=24, family='serif'), legend.title=element_blank(),strip.placement='outside', axis.text.y=element_blank(),legend.position='bottom')+
+   facet_grid(cols = vars(Years))#+

> # Output Figure 3
> ggsave(filename = 'figures/Fig3.png', width = 11, height=11,units = 'in',
+        plot = plot_grid(g,t,ncol=1,align='v'))

> rm(list=ls())

> gc()
            used   (Mb) gc trigger  (Mb)   max used    (Mb)
Ncells   1354629   72.4    2115057   113    2115057   113.0
Vcells 470056968 3586.3 2973494266 22686 3266513730 24921.6

> toc()
1235.265 sec elapsed

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   1354630   72.4    2115057   113.0    2115057   113.0
Vcells 470056964 3586.3 2378795413 18148.8 3266513730 24921.6

> # Figure 4
> tic() # time scripts for each figure

> source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   1356478   72.5    2115057   113.0    2115057   113.0
Vcells 470073976 3586.4 1903036331 14519.1 3266513730 24921.6

> ## ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)
Loading required package: Matrix

Attaching package: ‘Matrix’

The following objects are masked from ‘package:tidyr’:

    expand, pack, unpack


> library(stringr)

> data = fread(paste0('panel-analysis-2008-2012.csv.gz'),select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # create household change variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2426085  129.6    4234290   226.2    4234290   226.2
Vcells 950515842 7251.9 1522429065 11615.3 3266513730 24921.6

> # construct grouping variables based on age decile, gender, race, year1 treatment decile, state, year 1 household composition, and Zip Code
> # we can drop files where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))), include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> ## output SDs
> 
> 
> #   ###
> # estimate models
> 
> 
> dems.sd.DemSpExp = dems[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> dems.sd.RepSpExp = dems[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> reps.sd.DemSpExp = reps[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> reps.sd.RepSpExp = reps[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> oths.sd.DemSpExp = oths[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> oths.sd.RepSpExp = oths[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> out = list(dems.sd.DemSpExp, dems.sd.RepSpExp, reps.sd.DemSpExp,reps.sd.RepSpExp, oths.sd.DemSpExp, oths.sd.RepSpExp)

> names(out) = c('Democrats - Dem Exp',
+                'Democrats - Rep Exp',
+                'Republicans - Dem Exp',
+                'Republicans - Rep Exp',
+                'Non-partisans - Dem Exp',
+                'Non-partisans - Rep Exp')

> save(out, file = paste0('results/sds/sds-2008-2012-main-nohh.Rdata'))

> #   ###
> 
> # Estimate models
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)& !is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> # store output
> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2008-2012.Rdata'))

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2456153  131.2   26635524  1422.5   16372221   874.4
Vcells 567507951 4329.8 1753979083 13381.8 3266513730 24921.6

> source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2457886  131.3   21308420  1138.0   16372221   874.4
Vcells 567504784 4329.8 1753979083 13381.8 3266513730 24921.6

> ## ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # Create household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2466024  131.7   13637389   728.4   16372221   874.4
Vcells 1241290701 9470.3 2112530243 16117.4 3266513730 24921.6

> # construct grouping variables based on age decile, marital status, gender, race, year1 treatment decile, state, year 1 household composition, and Zip Code
> # we can drop files where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> dems.sd.DemSpExp = dems[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> dems.sd.RepSpExp = dems[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> reps.sd.DemSpExp = reps[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> reps.sd.RepSpExp = reps[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> oths.sd.DemSpExp = oths[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> oths.sd.RepSpExp = oths[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> out = list(dems.sd.DemSpExp, dems.sd.RepSpExp, reps.sd.DemSpExp,reps.sd.RepSpExp, oths.sd.DemSpExp, oths.sd.RepSpExp)

> names(out) = c('Democrats - Dem Exp',
+                'Democrats - Rep Exp',
+                'Republicans - Dem Exp',
+                'Republicans - Rep Exp',
+                'Non-partisans - Dem Exp',
+                'Non-partisans - Rep Exp')

> save(out, file = paste0('results/sds/sds-2012-2016-main-nohh.Rdata'))

> #   ###
> # Estimate models
> 
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016.Rdata'))

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2456179  131.2   30441386  1625.8   21593154  1153.2
Vcells 674223429 5144.0 2433775640 18568.3 3266513730 24921.6

> source('EST-current-results-2016-2020.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2457886  131.3   24353109  1300.6   21593154  1153.2
Vcells 674218117 5143.9 1947020512 14854.6 3266513730 24921.6

> ## ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # Create household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2466030   131.8   15585991   832.4   21593154  1153.2
Vcells 1531925044 11687.7 2336504614 17826.2 3266513730 24921.6

> # construct grouping variables based on age decile, marital status, gender, race, year1 treatment decile, state, year 1 household composition, and Zip Code
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> dems.sd.DemSpExp = dems[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> dems.sd.RepSpExp = dems[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> reps.sd.DemSpExp = reps[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> reps.sd.RepSpExp = reps[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> oths.sd.DemSpExp = oths[!grepl('_NA_',GroupSpDemExp),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

> oths.sd.RepSpExp = oths[!grepl('_NA_',GroupSpRepExp),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

> out = list(dems.sd.DemSpExp, dems.sd.RepSpExp, reps.sd.DemSpExp,reps.sd.RepSpExp, oths.sd.DemSpExp, oths.sd.RepSpExp)

> names(out) = c('Democrats - Dem Exp',
+                'Democrats - Rep Exp',
+                'Republicans - Dem Exp',
+                'Republicans - Rep Exp',
+                'Non-partisans - Dem Exp',
+                'Non-partisans - Rep Exp')

> save(out, file = paste0('results/sds/sds-2016-2020-main-nohh.Rdata'))

> #   ###
> # estimate models
> 
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020.Rdata'))

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2456179  131.2   38183526  2039.3   24283484  1296.9
Vcells 784042341 5981.8 3230216937 24644.6 4036452661 30795.7

> source('EST-current-results-2012-2016-pretrend.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2458992  131.4   30546821  1631.4   24283484  1296.9
Vcells 784045213 5981.8 2584173550 19715.7 4036452661 30795.7

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff',
+                                                                             'Party_2001','Party_2005','Party_2007','Party_2008','Party_2009',
+                                                                             'DemSpExp_nohh_2001','DemSpExp_nohh_2005','DemSpExp_nohh_2007','DemSpExp_nohh_2008','DemSpExp_nohh_2009',
+                                                                             'RepSpExp_nohh_2001','RepSpExp_nohh_2005','RepSpExp_nohh_2007','RepSpExp_nohh_2008','RepSpExp_nohh_2009'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

>   # Make household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Make party pre-period variables
> 
> # data[State=='CA',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]
> # data[State=='FL',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]
> # data[State=='NY',pid_pre := ifelse(Party_2008=='',NA,Party_2008)]
> # data[State=='NC',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]
> # data[State=='KS',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]
> 
> data[State=='CA',pid_pre1 := ifelse(Party_2005=='',NA,Party_2005)]

> data[State=='CA',pid_pre2 := ifelse(Party_2007=='',NA,Party_2007)]

> data[State=='CA',pid_pre3 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='FL',pid_pre1 := ifelse(Party_2007=='',NA,Party_2007)]

> data[State=='FL',pid_pre2 := ifelse(Party_2007=='',NA,Party_2007)]

> data[State=='FL',pid_pre3 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NY',pid_pre1 := ifelse(Party_2001=='',NA,Party_2001)]

> data[State=='NY',pid_pre2 := ifelse(Party_2001=='',NA,Party_2001)]

> data[State=='NY',pid_pre3 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='KS',pid_pre1 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='KS',pid_pre2 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='KS',pid_pre3 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='NC',pid_pre1 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NC',pid_pre2 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NC',pid_pre3 := ifelse(Party_2009=='',NA,Party_2009)]

> # make partisan exposure pre-period variables
> 
> # data[State=='CA',DemSpExp_nohh_pre := DemSpExp_nohh_2009]
> # data[State=='FL',DemSpExp_nohh_pre := DemSpExp_nohh_2009]
> # data[State=='NY',DemSpExp_nohh_pre := DemSpExp_nohh_2008]
> # data[State=='NC',DemSpExp_nohh_pre := DemSpExp_nohh_2009]
> # data[State=='KS',DemSpExp_nohh_pre := DemSpExp_nohh_2009]
> 
> data[State=='CA',DemSpExp_nohh_pre1 := DemSpExp_nohh_2005]

> data[State=='CA',DemSpExp_nohh_pre2 := DemSpExp_nohh_2007]

> data[State=='CA',DemSpExp_nohh_pre3 := DemSpExp_nohh_2009]

> # Only 2 pre period (2007,2009)
> data[State=='FL',DemSpExp_nohh_pre1 := DemSpExp_nohh_2007]

> data[State=='FL',DemSpExp_nohh_pre2 := DemSpExp_nohh_2007]

> data[State=='FL',DemSpExp_nohh_pre3 := DemSpExp_nohh_2009]

> # Only 2 pre period (2008, 2001)
> data[State=='NY',DemSpExp_nohh_pre1 := DemSpExp_nohh_2001]

> data[State=='NY',DemSpExp_nohh_pre2 := DemSpExp_nohh_2001]

> data[State=='NY',DemSpExp_nohh_pre3 := DemSpExp_nohh_2008]

> # Only one pre period (2008)
> data[State=='KS',DemSpExp_nohh_pre1 := DemSpExp_nohh_2008]

> data[State=='KS',DemSpExp_nohh_pre2 := DemSpExp_nohh_2008]

> data[State=='KS',DemSpExp_nohh_pre3 := DemSpExp_nohh_2008]

> # Only one pre period (2009)
> data[State=='NC',DemSpExp_nohh_pre1 := DemSpExp_nohh_2009]

> data[State=='NC',DemSpExp_nohh_pre2 := DemSpExp_nohh_2009]

> data[State=='Nc',DemSpExp_nohh_pre3 := DemSpExp_nohh_2009]

> data[State=='CA',RepSpExp_nohh_pre1 := RepSpExp_nohh_2005]

> data[State=='CA',RepSpExp_nohh_pre2 := RepSpExp_nohh_2007]

> data[State=='CA',RepSpExp_nohh_pre3 := RepSpExp_nohh_2009]

> data[State=='FL',RepSpExp_nohh_pre1 := RepSpExp_nohh_2007]

> data[State=='FL',RepSpExp_nohh_pre2 := RepSpExp_nohh_2007]

> data[State=='FL',RepSpExp_nohh_pre3 := RepSpExp_nohh_2009]

> data[State=='NY',RepSpExp_nohh_pre1 := RepSpExp_nohh_2001]

> data[State=='NY',RepSpExp_nohh_pre2 := RepSpExp_nohh_2001]

> data[State=='NY',RepSpExp_nohh_pre3 := RepSpExp_nohh_2008]

> data[State=='KS',RepSpExp_nohh_pre1 := RepSpExp_nohh_2008]

> data[State=='KS',RepSpExp_nohh_pre2 := RepSpExp_nohh_2008]

> data[State=='KS',RepSpExp_nohh_pre3 := RepSpExp_nohh_2008]

> data[State=='NC',RepSpExp_nohh_pre1 := RepSpExp_nohh_2009]

> data[State=='NC',RepSpExp_nohh_pre2 := RepSpExp_nohh_2009]

> data[State=='NC',RepSpExp_nohh_pre3 := RepSpExp_nohh_2009]

> #
> data = data[!is.na(DemSpExp_nohh_pre3)&!is.na(RepSpExp_nohh_pre3) & 
+             !is.na(pid_pre3) & !is.na(pid_pre2) & !is.na(pid_pre1) &
+             !is.na(DemSpExp_nohh_pre2)&!is.na(RepSpExp_nohh_pre2) &
+             !is.na(DemSpExp_nohh_pre1)&!is.na(RepSpExp_nohh_pre1) ]

> data[,DemSpPre1:=round(DemSpExp_nohh_pre1*100)]

> data[,RepSpPre1:=round(RepSpExp_nohh_pre1*100)]

> data[,DemSpPre2:=round(DemSpExp_nohh_pre2*100)]

> data[,RepSpPre2:=round(RepSpExp_nohh_pre2*100)]

> data[,DemSpPre3:=round(DemSpExp_nohh_pre3*100)]

> data[,RepSpPre3:=round(RepSpExp_nohh_pre3*100)]

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2463766  131.6   24437457  1305.2   24283484  1296.9
Vcells 1310515422 9998.5 2584173550 19715.7 4036452661 30795.7

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2463918  131.6   19549966  1044.1   24283484  1296.9
Vcells 1310518619 9998.5 2584173550 19715.7 4036452661 30795.7

>   # construct grouping variables based on state, year1 treatment decile, year1 household composition variables, and party and partisan exposure pretrend variables
>   
>   # drop rows where grouping variables from main spec are missing
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
> 
> dems[,GroupSpDemExp:=paste( DemSpDecile,
+                             DemSpPre1,DemSpPre2,DemSpPre3,
+                             pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(DemSpDecile,
+                            DemSpPre1,DemSpPre2,DemSpPre3,
+                            pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(DemSpDecile,
+                            DemSpPre1,DemSpPre2,DemSpPre3,
+                            pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,pid_pre1, pid_pre2, pid_pre3, hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,pid_pre1, pid_pre2, pid_pre3, hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

>   ## output SDs
>   
>   dems.sd.DemSpExp = dems[!is.na(DemSpExp_nohh_year1),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

>   dems.sd.RepSpExp = dems[!is.na(RepSpExp_nohh_year1),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

>   reps.sd.DemSpExp = reps[!is.na(DemSpExp_nohh_year1),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

>   reps.sd.RepSpExp = reps[!is.na(RepSpExp_nohh_year1),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

>   oths.sd.DemSpExp = oths[!is.na(DemSpExp_nohh_year1),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

>   oths.sd.RepSpExp = oths[!is.na(RepSpExp_nohh_year1),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

>   out = list(dems.sd.DemSpExp, dems.sd.RepSpExp, reps.sd.DemSpExp,reps.sd.RepSpExp, oths.sd.DemSpExp, oths.sd.RepSpExp)

> names(out) = c('Democrats - Dem Exp',
+                'Democrats - Rep Exp',
+                'Republicans - Dem Exp',
+                'Republicans - Rep Exp',
+                'Non-partisans - Dem Exp',
+                'Non-partisans - Rep Exp')

> save(out, file = paste0('results/sds/sds-2012-2016-nohh-pretrend.Rdata'))

> #   ###
>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-pretrend.Rdata'))

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2456288  131.2   15639973   835.3   24283484  1296.9
Vcells 943826675 7200.9 2584173550 19715.7 4036452661 30795.7

> source('EST-current-results-2016-2020-pretrend.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2458384  131.3   12511979   668.3   24283484  1296.9
Vcells 943817516 7200.8 2584173550 19715.7 4036452661 30795.7

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff',
+                                                                             'Party_2012','Party_2013','Party_2014','Party_2015',
+                                                                             'DemSpExp_nohh_2012','DemSpExp_nohh_2013','DemSpExp_nohh_2014','DemSpExp_nohh_2015',
+                                                                             'RepSpExp_nohh_2012','RepSpExp_nohh_2013','RepSpExp_nohh_2014','RepSpExp_nohh_2015'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

>   # Make household composition variables  
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Make party pre-period variables
> data[Party_2012=='',Party_2012:=NA]

> data[Party_2013=='',Party_2013:=NA]

> data[Party_2014=='',Party_2014:=NA]

> data[Party_2015=='',Party_2015:=NA]

> data[!Party_2012%in%c('Democrat','Republican'),Party_2012:='Non-Partisan']

> data[!Party_2013%in%c('Democrat','Republican'),Party_2013:='Non-Partisan']

> data[!Party_2014%in%c('Democrat','Republican'),Party_2014:='Non-Partisan']

> data[!Party_2015%in%c('Democrat','Republican'),Party_2015:='Non-Partisan']

> #
> data = data[!is.na(DemSpExp_nohh_2012)& !is.na(DemSpExp_nohh_2013)&!is.na(DemSpExp_nohh_2014)&!is.na(DemSpExp_nohh_2015)&
+             !is.na(RepSpExp_nohh_2012)& !is.na(RepSpExp_nohh_2013)&!is.na(RepSpExp_nohh_2014)&!is.na(RepSpExp_nohh_2015)&
+             !is.na(Party_2012)&!is.na(Party_2012)&!is.na(Party_2013)&!is.na(Party_2014)&!is.na(Party_2015)]

> # Make partisan exposure pre-period variables
> data[,DemSpPre1:=round(DemSpExp_nohh_2012*100)]

> data[,RepSpPre1:=round(RepSpExp_nohh_2012*100)]

> data[,DemSpPre2:=round(DemSpExp_nohh_2013*100)]

> data[,RepSpPre2:=round(RepSpExp_nohh_2013*100)]

> data[,DemSpPre3:=round(DemSpExp_nohh_2014*100)]

> data[,RepSpPre3:=round(RepSpExp_nohh_2014*100)]

> data[,DemSpPre4:=round(DemSpExp_nohh_2015*100)]

> data[,RepSpPre4:=round(RepSpExp_nohh_2015*100)]

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2466309   131.8    8007668   427.7   24283484  1296.9
Vcells 1825470050 13927.3 3101088260 23659.5 4036452661 30795.7

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2466471   131.8    8007668   427.7   24283484  1296.9
Vcells 1825486614 13927.4 3101088260 23659.5 4036452661 30795.7

>   # construct grouping variables based on state, year1 treatment decile, year1 household composition variables, and party and partisan exposure pretrend variables
>   
>   # drop rows where grouping variables from main spec are missing
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
> dems[,GroupSpDemExp:=paste( DemSpDecile,
+                             DemSpPre1,DemSpPre2,DemSpPre3,DemSpPre4,
+                             Party_2012, Party_2013, Party_2014, Party_2015,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(DemSpDecile,
+                            DemSpPre1,DemSpPre2,DemSpPre3,DemSpPre4,
+                            Party_2012, Party_2013, Party_2014, Party_2015,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(DemSpDecile,
+                            DemSpPre1,DemSpPre2,DemSpPre3,DemSpPre4,
+                            Party_2012, Party_2013, Party_2014, Party_2015,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,RepSpPre4,Party_2012, Party_2013, Party_2014, Party_2015,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,RepSpPre4,Party_2012, Party_2013, Party_2014, Party_2015,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,RepSpPre4,Party_2012, Party_2013, Party_2014, Party_2015,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

>   ## output SDs
>   
>   dems.sd.DemSpExp = dems[!is.na(DemSpExp_nohh_year1),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

>   dems.sd.RepSpExp = dems[!is.na(RepSpExp_nohh_year1),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

>   reps.sd.DemSpExp = reps[!is.na(DemSpExp_nohh_year1),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

>   reps.sd.RepSpExp = reps[!is.na(RepSpExp_nohh_year1),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

>   oths.sd.DemSpExp = oths[!is.na(DemSpExp_nohh_year1),list(n=.N, mean = mean(DemSpExpDiff_nohh,na.rm=T), sd = sd(DemSpExpDiff_nohh,na.rm=T)),by='GroupSpDemExp']

>   oths.sd.RepSpExp = oths[!is.na(RepSpExp_nohh_year1),list(n=.N, mean = mean(RepSpExpDiff_nohh,na.rm=T), sd = sd(RepSpExpDiff_nohh,na.rm=T)),by='GroupSpRepExp']

>   out = list(dems.sd.DemSpExp, dems.sd.RepSpExp, reps.sd.DemSpExp,reps.sd.RepSpExp, oths.sd.DemSpExp, oths.sd.RepSpExp)

> names(out) = c('Democrats - Dem Exp',
+                'Democrats - Rep Exp',
+                'Republicans - Dem Exp',
+                'Republicans - Rep Exp',
+                'Non-partisans - Dem Exp',
+                'Non-partisans - Rep Exp')

> save(out, file = paste0('results/sds/sds-2016-2020-nohh-pretrend.Rdata'))

> #   ###
>   # estimate models
>   
> 
>   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

>   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-pretrend.Rdata'))

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2456322  131.2   15523176   829.1   24283484  1296.9
Vcells 1108546483 8457.6 2977108730 22713.6 4036452661 30795.7

> source('EST-future-results-2008-2012.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2457874  131.3   12418541   663.3   24283484  1296.9
Vcells 1108540549 8457.5 2977108730 22713.6 4036452661 30795.7

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread(paste0('panel-analysis-2008-2012.csv.gz'),select = c('DemDiff','Party_2016','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> # Create year 2 Party variable
> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2012:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2012:='Republican']

> data[is.na(Party_2012), Party_2012:='Other']

> data[Party_2016=='',Party_2016:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # Make household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Create downstream party change variables
> data[,DemDiff2:=as.numeric(Party_2016=='Democrat')-as.numeric(Party_2012=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2016=='Republican')-as.numeric(Party_2012=='Republican')]

> # split data
> dems = data[Party_year1=='Democrat' & Party_2012=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2012=='Republican']

> oths = data[Party_year1=='Other' & Party_2012=='Other']

> rm(data)

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2464463   131.7    9934833   530.6   24283484  1296.9
Vcells 1639066219 12505.1 2977108730 22713.6 4036452661 30795.7

> # construct grouping variables based on age decile, gender, race, year1 treatment decile, and Zip Code
> # we can drop files where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> # Estimate models
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2008-2012.Rdata'))

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2456296  131.2   15977892   853.4   24283484  1296.9
Vcells 1199954802 9155.0 2977108730 22713.6 4036452661 30795.7

> source('EST-future-results-2012-2016.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2457986  131.3   12782314   682.7   24283484  1296.9
Vcells 1199964477 9155.1 2977108730 22713.6 4036452661 30795.7

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> # Create year 2 Party variable
> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # Make household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Create downstream party change variables
> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2466139   131.8   10225852   546.2   24283484  1296.9
Vcells 1942683314 14821.5 2977108730 22713.6 4036452661 30795.7

> # construct grouping variables based on age decile, gender, race, marital status, household composition, year1 treatment decile, and Zip Code
> # we can drop files where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016.Rdata'))

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2456398  131.2   32965576  1760.6   24283484  1296.9
Vcells 1302378613 9936.4 3572610476 27256.9 4036452661 30795.7

> source('EST-future-results-2012-2016-pretrend.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2459274  131.4   26372461  1408.5   24283484  1296.9
Vcells 1302395708 9936.5 3572610476 27256.9 4036452661 30795.7

> ## ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff','Party_2001','Party_2005','Party_2007','Party_2008','Party_2009',
+                                                                            'DemSpExp_nohh_2001','DemSpExp_nohh_2005','DemSpExp_nohh_2007','DemSpExp_nohh_2008','DemSpExp_nohh_2009',
+                                                                            'RepSpExp_nohh_2001','RepSpExp_nohh_2005','RepSpExp_nohh_2007','RepSpExp_nohh_2008','RepSpExp_nohh_2009'))
|--------------------------------------------------|
|==================================================|

> # Make year2 party variable
> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # Create household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Make downstream party change variable
> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # Make party pre-period variables
> data[State=='CA',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='FL',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NY',pid_pre := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='NC',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='KS',pid_pre := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='CA',pid_pre1 := ifelse(Party_2005=='',NA,Party_2005)]

> data[State=='CA',pid_pre2 := ifelse(Party_2007=='',NA,Party_2007)]

> data[State=='CA',pid_pre3 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='FL',pid_pre1 := ifelse(Party_2007=='',NA,Party_2007)]

> data[State=='FL',pid_pre2 := ifelse(Party_2007=='',NA,Party_2007)]

> data[State=='FL',pid_pre3 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NY',pid_pre1 := ifelse(Party_2001=='',NA,Party_2001)]

> data[State=='NY',pid_pre2 := ifelse(Party_2001=='',NA,Party_2001)]

> data[State=='NY',pid_pre3 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='KS',pid_pre1 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='KS',pid_pre2 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='KS',pid_pre3 := ifelse(Party_2008=='',NA,Party_2008)]

> data[State=='NC',pid_pre1 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NC',pid_pre2 := ifelse(Party_2009=='',NA,Party_2009)]

> data[State=='NC',pid_pre3 := ifelse(Party_2009=='',NA,Party_2009)]

> # make partisan exposure pre-period variables
> data[State=='CA',DemSpExp_nohh_pre := DemSpExp_nohh_2009]

> data[State=='FL',DemSpExp_nohh_pre := DemSpExp_nohh_2009]

> data[State=='NY',DemSpExp_nohh_pre := DemSpExp_nohh_2008]

> data[State=='NC',DemSpExp_nohh_pre := DemSpExp_nohh_2009]

> data[State=='KS',DemSpExp_nohh_pre := DemSpExp_nohh_2009]

> data[State=='CA',DemSpExp_nohh_pre1 := DemSpExp_nohh_2005]

> data[State=='CA',DemSpExp_nohh_pre2 := DemSpExp_nohh_2007]

> data[State=='CA',DemSpExp_nohh_pre3 := DemSpExp_nohh_2009]

> data[State=='FL',DemSpExp_nohh_pre1 := DemSpExp_nohh_2007]

> data[State=='FL',DemSpExp_nohh_pre2 := DemSpExp_nohh_2007]

> data[State=='FL',DemSpExp_nohh_pre3 := DemSpExp_nohh_2009]

> data[State=='NY',DemSpExp_nohh_pre1 := DemSpExp_nohh_2001]

> data[State=='NY',DemSpExp_nohh_pre2 := DemSpExp_nohh_2001]

> data[State=='NY',DemSpExp_nohh_pre3 := DemSpExp_nohh_2008]

> data[State=='KS',DemSpExp_nohh_pre1 := DemSpExp_nohh_2008]

> data[State=='KS',DemSpExp_nohh_pre2 := DemSpExp_nohh_2008]

> data[State=='KS',DemSpExp_nohh_pre3 := DemSpExp_nohh_2008]

> data[State=='NC',DemSpExp_nohh_pre1 := DemSpExp_nohh_2009]

> data[State=='NC',DemSpExp_nohh_pre2 := DemSpExp_nohh_2009]

> data[State=='Nc',DemSpExp_nohh_pre3 := DemSpExp_nohh_2009]

> data[State=='CA',RepSpExp_nohh_pre1 := RepSpExp_nohh_2005]

> data[State=='CA',RepSpExp_nohh_pre2 := RepSpExp_nohh_2007]

> data[State=='CA',RepSpExp_nohh_pre3 := RepSpExp_nohh_2009]

> data[State=='FL',RepSpExp_nohh_pre1 := RepSpExp_nohh_2007]

> data[State=='FL',RepSpExp_nohh_pre2 := RepSpExp_nohh_2007]

> data[State=='FL',RepSpExp_nohh_pre3 := RepSpExp_nohh_2009]

> data[State=='NY',RepSpExp_nohh_pre1 := RepSpExp_nohh_2001]

> data[State=='NY',RepSpExp_nohh_pre2 := RepSpExp_nohh_2001]

> data[State=='NY',RepSpExp_nohh_pre3 := RepSpExp_nohh_2008]

> data[State=='KS',RepSpExp_nohh_pre1 := RepSpExp_nohh_2008]

> data[State=='KS',RepSpExp_nohh_pre2 := RepSpExp_nohh_2008]

> data[State=='KS',RepSpExp_nohh_pre3 := RepSpExp_nohh_2008]

> data[State=='NC',RepSpExp_nohh_pre1 := RepSpExp_nohh_2009]

> data[State=='NC',RepSpExp_nohh_pre2 := RepSpExp_nohh_2009]

> data[State=='NC',RepSpExp_nohh_pre3 := RepSpExp_nohh_2009]

> #
> data = data[!is.na(DemSpExp_nohh_pre3)&!is.na(RepSpExp_nohh_pre3) & 
+               !is.na(pid_pre3) & !is.na(pid_pre2) & !is.na(pid_pre1) &
+               !is.na(DemSpExp_nohh_pre2)&!is.na(RepSpExp_nohh_pre2) &
+               !is.na(DemSpExp_nohh_pre1)&!is.na(RepSpExp_nohh_pre1) ]

> data[,DemSpPre1:=round(DemSpExp_nohh_pre1*100)]

> data[,RepSpPre1:=round(RepSpExp_nohh_pre1*100)]

> data[,DemSpPre2:=round(DemSpExp_nohh_pre2*100)]

> data[,RepSpPre2:=round(RepSpExp_nohh_pre2*100)]

> data[,DemSpPre3:=round(DemSpExp_nohh_pre3*100)]

> data[,RepSpPre3:=round(RepSpExp_nohh_pre3*100)]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2464196   131.7   21097969  1126.8   24283484  1296.9
Vcells 1856938609 14167.4 3572610476 27256.9 4036452661 30795.7

> # construct grouping variables based on state, year1 treatment decile, year1 household composition variables, and party and partisan exposure pretrend variables
> # we can drop rows where any of these are missing
> 
> 
> # Drop rows where grouping variables from main spec are missing
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste( DemSpDecile,
+                             DemSpPre1,DemSpPre2,DemSpPre3,
+                             pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(DemSpDecile,
+                            DemSpPre1,DemSpPre2,DemSpPre3,
+                            pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(DemSpDecile,
+                            DemSpPre1,DemSpPre2,DemSpPre3,
+                            pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,pid_pre1, pid_pre2, pid_pre3, hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,pid_pre1, pid_pre2, pid_pre3,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(RepSpDecile,RepSpPre1,RepSpPre2,RepSpPre3,pid_pre1, pid_pre2, pid_pre3, hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!is.na(DemSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!is.na(RepSpExp_nohh_year1)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-pretrend.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2456527   131.2   16878376   901.5   24283484  1296.9
Vcells 1466261929 11186.7 3572610476 27256.9 4036452661 30795.7

> source('FIG-current-results.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2457507   131.3   13502701   721.2   24283484  1296.9
Vcells 1466251928 11186.7 3572610476 27256.9 4036452661 30795.7

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c('current-results-2008-2012.Rdata',
+                                         'current-results-2012-2016.Rdata',
+                                         'current-results-2016-2020.Rdata',
+                                         'current-results-2012-2016-pretrend.Rdata',
+                                         'current-results-2016-2020-pretrend.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+ l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
+                     grepl('2012-2016',file)~ '2012-2016',
+                     grepl('2016-2020',file)~ '2016-2020')]  
+ l[,file:=file]
+ l[,Specification:=ifelse(grepl('pretrend',file),'Pretrend', 'Main')]
+ l[,Year1:=substr(Years,1,4)]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results, aes(y = Estimate, x = Subset, color = Subset, shape = Specification))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original party')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'), strip.text.y = element_text(size=12,angle=0),legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/Fig4a.png',dpi=300, width = 10, height = 5, units = 'in')

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2458759  131.4   25921329  1384.4   32401661  1730.5
Vcells 1050842265 8017.3 2858088381 21805.5 4036452661 30795.7

> source('FIG-future-results.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2458940  131.4   20737064  1107.5   32401661  1730.5
Vcells 1050532833 8015.0 2858088381 21805.5 4036452661 30795.7

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c('future-results-2008-2012.Rdata',
+                                         'future-results-2012-2016.Rdata',
+                                         'future-results-2012-2016-pretrend.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[,Years:=case_when(grepl('2008-2012',file)~ 'EV: 2008-2012 -- DV: 2012-2016',
+                       grepl('2012-2016',file)~ 'EV: 2012-2016 -- DV: 2016-2020')]  
+   l[,file:=file]
+   l[,Sample:=ifelse(grepl('-alt',file),'Alternative', 'Main')]
+   l[,Year1:=substr(Years,1,4)] 
+ 
+   l[,Specification:=ifelse(grepl('pretrend',file),'Pretrend', 'Main')]
+   
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results, aes(y = Estimate, x = Subset, color = Subset, shape = Specification))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original party')+
+   scale_color_manual(values = colors)+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'), strip.text.y = element_text(size=12,angle=0), legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/Fig4b.png',dpi=300, width = 10, height = 5, units = 'in')

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2459527  131.4   13271722   708.8   32401661  1730.5
Vcells 1050850492 8017.4 2858088381 21805.5 4036452661 30795.7

> toc()
8862.845 sec elapsed

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2458770  131.4   10617378   567.1   32401661  1730.5
Vcells 1050534239 8015.0 2858088381 21805.5 4036452661 30795.7

> # Figure 5
> tic() # time scripts for each figure

> source('EST-placebo-results-2016-2020.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2462265  131.5    8493903   453.7   32401661  1730.5
Vcells 1050565858 8015.2 2858088381 21805.5 4036452661 30795.7

> ## ESTIMATE TSMART PID PLACEBO PRE-PERIOD MODELS
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'Party_2015','Party_2014','Party_2013','Party_2012'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # Make household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Empty pre-period party values (i.e., voters for whom we have no information on that year's past partisanship) should be coded as NAs so they are dropped from the estimation for that year
> data[Party_2012=='',Party_2012:=NA]

> data[Party_2013=='',Party_2013:=NA]

> data[Party_2014=='',Party_2014:=NA]

> data[Party_2015=='',Party_2015:=NA]

> # Make pre-period party variables
> data[!is.na(Party_2012)&!Party_2012%in%c('Democrat','Republican'),Party_2012:='Non-Partisan']

> data[!is.na(Party_2013)&!Party_2013%in%c('Democrat','Republican'),Party_2013:='Non-Partisan']

> data[!is.na(Party_2014)&!Party_2014%in%c('Democrat','Republican'),Party_2014:='Non-Partisan']

> data[!is.na(Party_2015)&!Party_2015%in%c('Democrat','Republican'),Party_2014:='Non-Partisan']

> data[,Dem_2012 := as.numeric(Party_2012=='Democrat')]

> data[,Dem_2013 := as.numeric(Party_2013=='Democrat')]

> data[,Dem_2014 := as.numeric(Party_2014=='Democrat')]

> data[,Dem_2015 := as.numeric(Party_2015=='Democrat')]

> data[,Rep_2012 := as.numeric(Party_2012=='Republican')]

> data[,Rep_2013 := as.numeric(Party_2013=='Republican')]

> data[,Rep_2014 := as.numeric(Party_2014=='Republican')]

> data[,Rep_2015 := as.numeric(Party_2015=='Republican')]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used  (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2470446   132    6795123   362.9   32401661  1730.5
Vcells 2244597527 17125 3435717696 26212.5 4036452661 30795.7

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>       # we can drop files where any of these are missing
> 
>           
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ###
>   # estimate models
>   
> # 2012
> ModelDemSpExpDems_2012 = summary(felm(Dem_2012 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps_2012 = summary(felm(Dem_2012 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths_2012 = summary(felm(Dem_2012 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems_2012 = summary(felm(Rep_2012 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps_2012 = summary(felm(Rep_2012 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths_2012 = summary(felm(Rep_2012 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> #2013
> ModelDemSpExpDems_2013 = summary(felm(Dem_2013 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps_2013 = summary(felm(Dem_2013 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths_2013 = summary(felm(Dem_2013 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems_2013 = summary(felm(Rep_2013 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps_2013 = summary(felm(Rep_2013 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths_2013 = summary(felm(Rep_2013 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> #2014
> ModelDemSpExpDems_2014 = summary(felm(Dem_2014 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps_2014 = summary(felm(Dem_2014 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths_2014 = summary(felm(Dem_2014 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems_2014 = summary(felm(Rep_2014 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps_2014 = summary(felm(Rep_2014 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths_2014 = summary(felm(Rep_2014 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> #2015
> ModelDemSpExpDems_2015 = summary(felm(Dem_2015 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps_2015 = summary(felm(Dem_2015 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths_2015 = summary(felm(Dem_2015 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems_2015 = summary(felm(Rep_2015 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps_2015 = summary(felm(Rep_2015 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths_2015 = summary(felm(Rep_2015 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> #
> 
> save(ModelDemSpExpDems_2015, ModelDemSpExpReps_2015, ModelDemSpExpOths_2015, 
+      ModelRepSpExpDems_2015, ModelRepSpExpReps_2015, ModelRepSpExpOths_2015,
+      
+      ModelDemSpExpDems_2014, ModelDemSpExpReps_2014, ModelDemSpExpOths_2014, 
+      ModelRepSpExpDems_2014, ModelRepSpExpReps_2014, ModelRepSpExpOths_2014,
+      
+      ModelDemSpExpDems_2013, ModelDemSpExpReps_2013, ModelDemSpExpOths_2013, 
+      ModelRepSpExpDems_2013, ModelRepSpExpReps_2013, ModelRepSpExpOths_2013,
+      
+      ModelDemSpExpDems_2012, ModelDemSpExpReps_2012, ModelDemSpExpOths_2012, 
+      ModelRepSpExpDems_2012, ModelRepSpExpReps_2012, ModelRepSpExpOths_2012,
+      
+      file = 'results/placebo-results-2016-2020.Rdata')

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2458945  131.4   39678793  2119.1   32401661  1730.5
Vcells 1174089100 8957.6 3958087586 30197.9 4947536053 37746.8

> source('FIG-placebo.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2460608  131.5   31743035  1695.3   32401661  1730.5
Vcells 1174094233 8957.7 3166470069 24158.3 4947536053 37746.8

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> library(tidyr)

> files = c('results/placebo-results-2016-2020.Rdata',
+ 'results/current-results-2016-2020.Rdata')

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate%in%c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[grepl('placebo',file),Placebo.Year:=substr(Model,19,22)]

> results[!grepl('placebo',file),Placebo.Year:='2020']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> holder = results[1:12,]

> holder[1,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','...','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')

> holder[2,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','...','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

> holder[3,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','2016','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')

> holder[4,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','2016','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

> holder[5,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','...','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')

> holder[6,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','...','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

> holder[7,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','2016','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')

> holder[8,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','2016','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

> holder[9,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','...','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')

> holder[10,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','...','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

> holder[11,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','2016','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')

> holder[12,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','2016','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

> results=rbind(results,holder)

> results$Placebo.Year=str_replace(results$Placebo.Year,'20','\'')

> results$Placebo.Year=factor(results$Placebo.Year,levels = str_replace(as.character(c(2012:2016, '...', '2020')),'20','\''))

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = 'purple')

> g = ggplot(results, aes(y = Estimate, x = Placebo.Year, color = Subset))+
+   geom_point(size = 5)+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   theme_bw()+
+   facet_grid(Exposure.Type~Subset)+
+   geom_vline(xintercept='\'16', linetype='dotted')+
+   scale_color_manual(values=colors)+
+   ylab('Effect on partisan registration')+
+   xlab('Placebo year')+
+   guides(color = 'none',shape='none')+
+   theme(text = element_text(size=15,family='serif'),strip.text.y = element_text(size=12, angle = 0), legend.title = element_blank(), legend.position = 'bottom')

> ggsave(plot = g, filename = 'figures/Fig5.png', dpi= 300, width = 11, height = 5, units = 'in')

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2465302  131.7   25394428  1356.3   32401661  1730.5
Vcells 1175511029 8968.5 3166470069 24158.3 4947536053 37746.8

> toc()
4370.164 sec elapsed

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2460754  131.5   20315543  1085.0   32401661  1730.5
Vcells 1174100126 8957.7 3166470069 24158.3 4947536053 37746.8

> # Figure 6 (also creates Figure S2)
> tic() # time scripts for each figure

> source('EST-current-results-2016-2020-dwelling-age.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2462518  131.6   16252435   868.0   32401661  1730.5
Vcells 1174115982 8957.8 3166470069 24158.3 4947536053 37746.8

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'vb.tsmart_dwelling_type'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> # Create household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Make age strata variable
> data[,Age_strata := ifelse(Age_year1 <= 34, '18-34', 
+                            ifelse(Age_year1 %in% 35:49, '35-49',
+                                   ifelse(Age_year1 %in% 50:64, '50-64', '65+')))]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   #rm(data)
>   gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2470735   132.0   13001948   694.4   32401661  1730.5
Vcells 2945542880 22472.8 3799844082 28990.6 4947536053 37746.8

>   # construct grouping variables based on age decile, marital status, gender, race, year1 treatment decile, state, year 1 household composition, and Zip Code
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems1=dems

> reps1=reps

> oths1=oths

> for(i in c('S','H')){
+   for(j in unique(data$Age_strata)[!is.na(unique(data$Age_strata))]){
+     dems=dems1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     reps=reps1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     oths=oths1[vb.tsmart_dwelling_type==i & Age_strata==j]
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-',i,'-',j,'.Rdata'))
+   }
+ }

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2461472  131.5   33500770  1789.2   32401661  1730.5
Vcells 1288663587 9831.8 4377561182 33398.2 5471832705 41746.8

> source('EST-current-results-2012-2016-dwelling-age.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2462810  131.6   26800616  1431.4   32401661  1730.5
Vcells 1288509266 9830.6 3502048946 26718.6 5471832705 41746.8

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'vb.tsmart_dwelling_type'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

>   # Create household composition variables  
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # Create age strata variables
> data[,Age_strata := ifelse(Age_year1 <= 34, '18-34', 
+                            ifelse(Age_year1 %in% 35:49, '35-49',
+                                   ifelse(Age_year1 %in% 50:64, '50-64', '65+')))]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>  # rm(data)
>   gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2471098   132.0   21440493  1145.1   32401661  1730.5
Vcells 2654668126 20253.6 4202538735 32062.9 5471832705 41746.8

>   # construct grouping variables based on age decile, marital status, gender, race, year1 treatment decile, state, year 1 household composition, and Zip Code
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems1=dems

> reps1=reps

> oths1=oths

> for(i in c('S','H')){
+   for(j in unique(data$Age_strata)[!is.na(unique(data$Age_strata))]){
+     dems=dems1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     reps=reps1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     oths=oths1[vb.tsmart_dwelling_type==i & Age_strata==j]
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-',i,'-',j,'.Rdata'))
+   }
+ }

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2461344   131.5   25100604  1340.6   32401661  1730.5
Vcells 1391032518 10612.8 4034501186 30780.9 5471832705 41746.8

> source('FIG-current-results-age-dwelling.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2462352   131.6   20080484  1072.5   32401661  1730.5
Vcells 1390925427 10612.0 4034501186 30780.9 5471832705 41746.8

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c("current-results-2012-2016-S-18-34.Rdata",
+                                         "current-results-2012-2016-S-35-49.Rdata",
+                                         "current-results-2012-2016-S-50-64.Rdata",
+                                         "current-results-2012-2016-S-65+.Rdata",
+                                         
+                                         "current-results-2012-2016-H-18-34.Rdata",
+                                         "current-results-2012-2016-H-35-49.Rdata",
+                                         "current-results-2012-2016-H-50-64.Rdata",
+                                         "current-results-2012-2016-H-65+.Rdata",
+   
+   
+                                         "current-results-2016-2020-S-18-34.Rdata",
+                                         "current-results-2016-2020-S-35-49.Rdata",
+                                         "current-results-2016-2020-S-50-64.Rdata",
+                                         "current-results-2016-2020-S-65+.Rdata",
+                                         
+                                         "current-results-2016-2020-H-18-34.Rdata",
+                                         "current-results-2016-2020-H-35-49.Rdata",
+                                         "current-results-2016-2020-H-50-64.Rdata",
+                                         "current-results-2016-2020-H-65+.Rdata"
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('-H-',file),Housing:='Apartment']

> results[grepl('-S-',file),Housing:='Single-family']

> results[grepl('18-34',file),Age:='18-34']

> results[grepl('35-49',file),Age:='35-49']

> results[grepl('50-64',file),Age:='50-64']

> results[grepl('65+',file),Age:='65+']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results[Years=='2016-2020'], aes(y = Estimate, x = Age, color = Subset, shape = Housing))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('2016 Age')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Subset)

> ggsave(plot = g1,filename = 'figures/Fig6.png',dpi=300, width = 11, height = 5, units = 'in')

> g2 = ggplot(results[Years=='2012-2016'], aes(y = Estimate, x = Age, color = Subset, shape = Housing))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('2012 Age')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Subset)

> ggsave(plot = g2,filename = 'figures/FigS2.png',dpi=300, width = 11, height = 5, units = 'in')

> toc()
3204.079 sec elapsed

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2464162   131.7   12851511   686.4   32401661  1730.5
Vcells 1390974702 10612.3 4034501186 30780.9 5471832705 41746.8

> # Figure 7 (also creates Figure S6)
> tic() # time scripts for each figure

> source('EST-current-results-2016-2020-race-subset.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2463988   131.6   10281209   549.1   32401661  1730.5
Vcells 1390948193 10612.1 4034501186 30780.9 5471832705 41746.8

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff',"DemSpExpWhite_nohh_year1" ,   "DemSpExpWhiteDiff_nohh"  ,    "DemSpExpBlack_nohh_year1" ,   "DemSpExpBlackDiff_nohh"     ,
+                                                                            "DemSpExpHispanic_nohh_year1", "DemSpExpHispanicDiff_nohh"  , "DemSpExpAsian_nohh_year1"  ,  "DemSpExpAsianDiff_nohh"  ,    "RepSpExpWhite_nohh_year1" ,  
+                                                                             "RepSpExpWhiteDiff_nohh"  ,    "RepSpExpBlack_nohh_year1"   , "RepSpExpBlackDiff_nohh"   ,   "RepSpExpHispanic_nohh_year1", "RepSpExpHispanicDiff_nohh",  
+                                                                             "RepSpExpAsian_nohh_year1" ,   "RepSpExpAsianDiff_nohh",'Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data = data[Race%in%c('White','Black','Hispanic','Asian')]

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

>   # create household change variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> for(r in c('Asian', 'Black','Hispanic','White')){
+   data[['DemTreatment']] = data[[paste0('DemSpExp', r,'_nohh_year1')]]
+   data[['RepTreatment']] = data[[paste0('RepSpExp', r,'_nohh_year1')]]
+   data[['DemSpExpDiff_nohh']] = data[[paste0('DemSpExp', r,'Diff_nohh')]]
+   data[['RepSpExpDiff_nohh']] = data[[paste0('RepSpExp', r,'Diff_nohh')]]
+   
+   
+   data[, Interaction:=ifelse(Race==r, r, paste0('Non-',r))]
+   
+   
+   # split data
+   dems = data[Party_year1=='Democrat']
+   reps = data[Party_year1=='Republican']
+   oths = data[Party_year1=='Other']
+   
+   #rm(data)
+   gc()
+   
+   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
+   # we can drop files where any of these are missing
+   
+   
+   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]
+   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+   
+   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+ 
+   # spatial seg treatment
+   # democrat exposure
+   dems[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+   # republican exposure
+   dems[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+   
+   
+   #####
+   # make groups
+   # spatial seg treatment
+   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   
+ dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ 
+   
+   
+ #   ###
+   # estimate models
+   
+   ModelDemSpExpDems = summary(felm(DemDiff ~ Interaction:(DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff ~ Interaction:(DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff ~ Interaction:(DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-',r,'-subset.Rdata'))
+ }

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2463795   131.6   45507259  2430.4   38489943  2055.6
Vcells 1572449314 11996.9 6693096878 51064.3 8366301193 63829.9

> source('EST-current-results-2012-2016-race-subset.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2465528   131.7   36405808  1944.3   38489943  2055.6
Vcells 1572453199 11996.9 5354477503 40851.5 8366301193 63829.9

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff',"DemSpExpWhite_nohh_year1" ,   "DemSpExpWhiteDiff_nohh"  ,    "DemSpExpBlack_nohh_year1" ,   "DemSpExpBlackDiff_nohh"     ,
+                                                                            "DemSpExpHispanic_nohh_year1", "DemSpExpHispanicDiff_nohh"  , "DemSpExpAsian_nohh_year1"  ,  "DemSpExpAsianDiff_nohh"  ,    "RepSpExpWhite_nohh_year1" ,  
+                                                                             "RepSpExpWhiteDiff_nohh"  ,    "RepSpExpBlack_nohh_year1"   , "RepSpExpBlackDiff_nohh"   ,   "RepSpExpHispanic_nohh_year1", "RepSpExpHispanicDiff_nohh",  
+                                                                             "RepSpExpAsian_nohh_year1" ,   "RepSpExpAsianDiff_nohh",'Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data = data[Race%in%c('White','Black','Hispanic','Asian')]

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> # Create household composition variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> for(r in c('Asian', 'Black','Hispanic','White')){
+   data[['DemTreatment']] = data[[paste0('DemSpExp', r,'_nohh_year1')]]
+   data[['RepTreatment']] = data[[paste0('RepSpExp', r,'_nohh_year1')]]
+   data[['DemSpExpDiff_nohh']] = data[[paste0('DemSpExp', r,'Diff_nohh')]]
+   data[['RepSpExpDiff_nohh']] = data[[paste0('RepSpExp', r,'Diff_nohh')]]
+   
+   
+   data[, Interaction:=ifelse(Race==r, r, paste0('Non-',r))]
+   
+   
+   # split data
+   dems = data[Party_year1=='Democrat']
+   reps = data[Party_year1=='Republican']
+   oths = data[Party_year1=='Other']
+   
+   #rm(data)
+   gc()
+   
+   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
+   # we can drop files where any of these are missing
+   
+   
+   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]
+   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+   
+   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+ 
+   # spatial seg treatment
+   # democrat exposure
+   dems[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+   # republican exposure
+   dems[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+   
+   
+   #####
+   # make groups
+   # spatial seg treatment
+   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   
+ dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ 
+   
+   
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff ~ Interaction:(DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff ~ Interaction:(DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff ~ Interaction:(DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-',r,'-subset.Rdata'))
+ }

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2463833   131.6   48996452  2616.7   38489943  2055.6
Vcells 1719936392 13122.1 6187128014 47204.1 8366301193 63829.9

> source('FIG-current-results-race-subset.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2465117   131.7   39197162  2093.4   38489943  2055.6
Vcells 1719935141 13122.1 4949702412 37763.3 8366301193 63829.9

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> library(patchwork)

Attaching package: ‘patchwork’

The following object is masked from ‘package:cowplot’:

    align_plots


> library(tidyverse)
── Attaching packages ──────────────────────────────────── tidyverse 1.3.1 ──
✔ readr   2.1.4     ✔ forcats 0.5.1
✔ purrr   1.0.2     
── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::between()    masks data.table::between()
✖ readr::col_factor() masks scales::col_factor()
✖ purrr::discard()    masks scales::discard()
✖ Matrix::expand()    masks tidyr::expand()
✖ dplyr::filter()     masks stats::filter()
✖ dplyr::first()      masks data.table::first()
✖ dplyr::lag()        masks stats::lag()
✖ dplyr::last()       masks data.table::last()
✖ Matrix::pack()      masks tidyr::pack()
✖ purrr::transpose()  masks data.table::transpose()
✖ Matrix::unpack()    masks tidyr::unpack()

> files = paste0('results/',c('current-results-2012-2016-Asian-subset.Rdata',
+                                         'current-results-2016-2020-Asian-subset.Rdata',
+                                         'current-results-2012-2016-Black-subset.Rdata',
+                                         'current-results-2016-2020-Black-subset.Rdata',
+                                         'current-results-2012-2016-Hispanic-subset.Rdata',
+                                         'current-results-2016-2020-Hispanic-subset.Rdata',
+                                         'current-results-2012-2016-White-subset.Rdata',
+                                         'current-results-2016-2020-White-subset.Rdata'
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('InteractionWhite:DemSpExpDiff_nohh','InteractionWhite:RepSpExpDiff_nohh',
+                                    'InteractionNon-White:DemSpExpDiff_nohh','InteractionNon-White:RepSpExpDiff_nohh',
+                                    
+                                    'InteractionBlack:DemSpExpDiff_nohh','InteractionBlack:RepSpExpDiff_nohh',
+                                    'InteractionNon-Black:DemSpExpDiff_nohh','InteractionNon-Black:RepSpExpDiff_nohh',
+                                    
+                                    
+                                    'InteractionHispanic:DemSpExpDiff_nohh','InteractionHispanic:RepSpExpDiff_nohh',
+                                    'InteractionNon-Hispanic:DemSpExpDiff_nohh','InteractionNon-Hispanic:RepSpExpDiff_nohh',
+                                    
+                                    
+                                    'InteractionAsian:DemSpExpDiff_nohh','InteractionAsian:RepSpExpDiff_nohh',
+                                    'InteractionNon-Asian:DemSpExpDiff_nohh','InteractionNon-Asian:RepSpExpDiff_nohh'
+                                    )]

> results[!grepl('Non-',Covariate),Type:='Ingroup']

> results[grepl('Non-',Covariate),Type:='Outgroup']

> results[grepl('White',file),Race:='White']

> results[grepl('Asian',file),Race:='Asian']

> results[grepl('Black',file),Race:='Black']

> results[grepl('Hispanic',file),Race:='Hispanic']

> #results[grepl('DemSpExp',Model),Exposure.Type:='Exposure to Democrats\non Democratic registration']
> #results[grepl('RepSpExp',Model),Exposure.Type:='Exposure to Republicans\non Republican registration']
> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g = ggplot(results[Years=='2016-2020'] %>%
+              mutate(Race = paste0(Race,' partisan exposure')), aes(y = Estimate, x = Subset, color = Subset, shape = Type))+
+   geom_point( size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Effect of partisan exposure')+
+   xlab('Original party')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'),panel.spacing = unit(.5, "lines"), strip.text.y = element_text(size = 10, angle = 0), legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Race, scales = 'free_x')

> ggsave(plot = g,filename = 'figures/Fig7.png',dpi=300, width = 13, height = 5, units = 'in')

> #2012-2016
> 
> 
> g = ggplot(results[Years=='2012-2016'] %>%
+              mutate(Race = paste0(Race,' partisan exposure')), aes(y = Estimate, x = Subset, color = Subset, shape = Type))+
+   geom_point( size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Effect of partisan exposure')+
+   xlab('Original party')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'),panel.spacing = unit(.5, "lines"), strip.text.y = element_text(size = 10, angle = 0), legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Race, scales = 'free_x')

> ggsave(plot = g,filename = 'figures/FigS6.png',dpi=300, width = 13, height = 5, units = 'in')

> toc()
10834.389 sec elapsed

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2741518   146.5   25086184  1339.8   39197162  2093.4
Vcells 1720738066 13128.2 4949702412 37763.3 8366301193 63829.9

> # Figure 8 (also creates Figure S)
> tic() # time scripts for each figure

> source('EST-current-results-2016-2020-by-district.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2742270   146.5   20068948  1071.8   39197162  2093.4
Vcells 1720389487 13125.6 4949702412 37763.3 8366301193 63829.9

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff','StateDistrict'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # create household change variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> #rm(data)
> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2750596   146.9   16055159   857.5   39197162  2093.4
Vcells 3433154125 26192.9 4949702412 37763.3 8366301193 63829.9

> # construct grouping variables
> # we can drop rows where any of these variables are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> dems1=dems

> reps1=reps

> oths1=oths

> for(s in unique(data$StateDistrict)[!is.na(unique(data$StateDistrict))]){
+   
+   dems = dems1[ StateDistrict ==s]
+   reps = reps1[ StateDistrict==s]
+   oths = oths1[  StateDistrict==s]
+   
+ 
+ ModelDemSpExpDems = try(summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+ ModelDemSpExpReps = try(summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+ ModelDemSpExpOths = try(summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+ 
+ ModelRepSpExpDems = try(summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+ ModelRepSpExpReps = try(summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+ ModelRepSpExpOths = try(summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-district-',s,'.Rdata'))
+ 
+ rm(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+    ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths)
+ 
+ }
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 0 (non-NA) cases; no valid data
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 0 (non-NA) cases; no valid data
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
4: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
5: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
6: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
7: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
4: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2741471   146.5   25813138  1378.6   39197162  2093.4
Vcells 1835136176 14001.0 5939722894 45316.5 8366301193 63829.9

> source('EST-current-results-2012-2016-by-district.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2742221   146.5   20650511  1102.9   39197162  2093.4
Vcells 1834782068 13998.3 5939722894 45316.5 8366301193 63829.9

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff','StateDistrict'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> # create household change variables
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> #rm(data)
> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2750687   147.0   16520409   882.3   39197162  2093.4
Vcells 3155835732 24077.2 5939722894 45316.5 8366301193 63829.9

> # construct grouping variables
> # we can drop rows where any of these variables are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> #   ###
> # estimate models
> 
> # spatial seg treatment
> 
> dems1=dems

> reps1=reps

> oths1=oths

> for(s in unique(data$StateDistrict)[!is.na(unique(data$StateDistrict))]){
+   
+   dems = dems1[ StateDistrict ==s]
+   reps = reps1[ StateDistrict==s]
+   oths = oths1[  StateDistrict==s]
+   
+   
+ ModelDemSpExpDems = try(summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+ ModelDemSpExpReps = try(summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+ ModelDemSpExpOths = try(summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+ 
+ ModelRepSpExpDems = try(summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+ ModelRepSpExpReps = try(summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+ ModelRepSpExpOths = try(summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-district-',s,'.Rdata'))
+ 
+ rm(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+    ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths)
+ }
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
4: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
5: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
6: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
7: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
4: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
5: In pt(abs(z$STATS[[lhs]]$tval), z$df, lower.tail = FALSE) :
  NaNs produced
6: In pf(F, p - hasicpt, rdf, lower.tail = FALSE) : NaNs produced
7: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(RepDiff ~ RepSpExpDiff_nohh + hh.r.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff + hh.n.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2740922   146.4   26523370  1416.6   39197162  2093.4
Vcells 1937368910 14781.0 5939722894 45316.5 8366301193 63829.9

> source('FIG-current-results-districts.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2742112   146.5   21218696  1133.3   39197162  2093.4
Vcells 1937201287 14779.7 5939722894 45316.5 8366301193 63829.9

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> library(scales)

> library(tidyr)

> files = paste0('results/',list.files('results'))

> files = files[grepl('district', files)]

> files = files[(grepl('2008',files)&grepl('2012',files))|(grepl('2012',files) & grepl('2016',files))|(grepl('2016',files)&grepl('2020',files))]

> files = files[!grepl('NA.Rdata',files)]

> files = files[!grepl('aspatial',files)]

> files = files[grepl('current',files)]

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   print(file)
+   l = rbindlist(lapply(models, FUN=function(x){
+     #print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,district:=str_replace(str_replace(file,paste0('results/current-results-',Year1, '-',Year2,'-district-'),''),'.Rdata','')]
+ l[,file:=file]
+ 
+ return(l)
+ }))
[1] "results/current-results-2012-2016-district-CA1.Rdata"
[1] "results/current-results-2012-2016-district-CA10.Rdata"
[1] "results/current-results-2012-2016-district-CA11.Rdata"
[1] "results/current-results-2012-2016-district-CA12.Rdata"
[1] "results/current-results-2012-2016-district-CA13.Rdata"
[1] "results/current-results-2012-2016-district-CA14.Rdata"
[1] "results/current-results-2012-2016-district-CA15.Rdata"
[1] "results/current-results-2012-2016-district-CA16.Rdata"
[1] "results/current-results-2012-2016-district-CA17.Rdata"
[1] "results/current-results-2012-2016-district-CA18.Rdata"
[1] "results/current-results-2012-2016-district-CA19.Rdata"
[1] "results/current-results-2012-2016-district-CA2.Rdata"
[1] "results/current-results-2012-2016-district-CA20.Rdata"
[1] "results/current-results-2012-2016-district-CA21.Rdata"
[1] "results/current-results-2012-2016-district-CA22.Rdata"
[1] "results/current-results-2012-2016-district-CA23.Rdata"
[1] "results/current-results-2012-2016-district-CA24.Rdata"
[1] "results/current-results-2012-2016-district-CA25.Rdata"
[1] "results/current-results-2012-2016-district-CA26.Rdata"
[1] "results/current-results-2012-2016-district-CA27.Rdata"
[1] "results/current-results-2012-2016-district-CA28.Rdata"
[1] "results/current-results-2012-2016-district-CA29.Rdata"
[1] "results/current-results-2012-2016-district-CA3.Rdata"
[1] "results/current-results-2012-2016-district-CA30.Rdata"
[1] "results/current-results-2012-2016-district-CA31.Rdata"
[1] "results/current-results-2012-2016-district-CA32.Rdata"
[1] "results/current-results-2012-2016-district-CA33.Rdata"
[1] "results/current-results-2012-2016-district-CA34.Rdata"
[1] "results/current-results-2012-2016-district-CA35.Rdata"
[1] "results/current-results-2012-2016-district-CA36.Rdata"
[1] "results/current-results-2012-2016-district-CA37.Rdata"
[1] "results/current-results-2012-2016-district-CA38.Rdata"
[1] "results/current-results-2012-2016-district-CA39.Rdata"
[1] "results/current-results-2012-2016-district-CA4.Rdata"
[1] "results/current-results-2012-2016-district-CA40.Rdata"
[1] "results/current-results-2012-2016-district-CA41.Rdata"
[1] "results/current-results-2012-2016-district-CA42.Rdata"
[1] "results/current-results-2012-2016-district-CA43.Rdata"
[1] "results/current-results-2012-2016-district-CA44.Rdata"
[1] "results/current-results-2012-2016-district-CA45.Rdata"
[1] "results/current-results-2012-2016-district-CA46.Rdata"
[1] "results/current-results-2012-2016-district-CA47.Rdata"
[1] "results/current-results-2012-2016-district-CA48.Rdata"
[1] "results/current-results-2012-2016-district-CA49.Rdata"
[1] "results/current-results-2012-2016-district-CA5.Rdata"
[1] "results/current-results-2012-2016-district-CA50.Rdata"
[1] "results/current-results-2012-2016-district-CA51.Rdata"
[1] "results/current-results-2012-2016-district-CA52.Rdata"
[1] "results/current-results-2012-2016-district-CA53.Rdata"
[1] "results/current-results-2012-2016-district-CA6.Rdata"
[1] "results/current-results-2012-2016-district-CA7.Rdata"
[1] "results/current-results-2012-2016-district-CA8.Rdata"
[1] "results/current-results-2012-2016-district-CA9.Rdata"
[1] "results/current-results-2012-2016-district-FL1.Rdata"
[1] "results/current-results-2012-2016-district-FL10.Rdata"
[1] "results/current-results-2012-2016-district-FL11.Rdata"
[1] "results/current-results-2012-2016-district-FL12.Rdata"
[1] "results/current-results-2012-2016-district-FL13.Rdata"
[1] "results/current-results-2012-2016-district-FL14.Rdata"
[1] "results/current-results-2012-2016-district-FL15.Rdata"
[1] "results/current-results-2012-2016-district-FL16.Rdata"
[1] "results/current-results-2012-2016-district-FL17.Rdata"
[1] "results/current-results-2012-2016-district-FL18.Rdata"
[1] "results/current-results-2012-2016-district-FL19.Rdata"
[1] "results/current-results-2012-2016-district-FL2.Rdata"
[1] "results/current-results-2012-2016-district-FL20.Rdata"
[1] "results/current-results-2012-2016-district-FL21.Rdata"
[1] "results/current-results-2012-2016-district-FL22.Rdata"
[1] "results/current-results-2012-2016-district-FL23.Rdata"
[1] "results/current-results-2012-2016-district-FL24.Rdata"
[1] "results/current-results-2012-2016-district-FL25.Rdata"
[1] "results/current-results-2012-2016-district-FL26.Rdata"
[1] "results/current-results-2012-2016-district-FL27.Rdata"
[1] "results/current-results-2012-2016-district-FL3.Rdata"
[1] "results/current-results-2012-2016-district-FL4.Rdata"
[1] "results/current-results-2012-2016-district-FL5.Rdata"
[1] "results/current-results-2012-2016-district-FL6.Rdata"
[1] "results/current-results-2012-2016-district-FL7.Rdata"
[1] "results/current-results-2012-2016-district-FL8.Rdata"
[1] "results/current-results-2012-2016-district-FL9.Rdata"
[1] "results/current-results-2012-2016-district-KS1.Rdata"
[1] "results/current-results-2012-2016-district-KS2.Rdata"
[1] "results/current-results-2012-2016-district-KS3.Rdata"
[1] "results/current-results-2012-2016-district-KS4.Rdata"
[1] "results/current-results-2012-2016-district-NC1.Rdata"
[1] "results/current-results-2012-2016-district-NC10.Rdata"
[1] "results/current-results-2012-2016-district-NC11.Rdata"
[1] "results/current-results-2012-2016-district-NC12.Rdata"
[1] "results/current-results-2012-2016-district-NC13.Rdata"
[1] "results/current-results-2012-2016-district-NC2.Rdata"
[1] "results/current-results-2012-2016-district-NC3.Rdata"
[1] "results/current-results-2012-2016-district-NC4.Rdata"
[1] "results/current-results-2012-2016-district-NC5.Rdata"
[1] "results/current-results-2012-2016-district-NC6.Rdata"
[1] "results/current-results-2012-2016-district-NC7.Rdata"
[1] "results/current-results-2012-2016-district-NC8.Rdata"
[1] "results/current-results-2012-2016-district-NC9.Rdata"
[1] "results/current-results-2012-2016-district-NY1.Rdata"
[1] "results/current-results-2012-2016-district-NY10.Rdata"
[1] "results/current-results-2012-2016-district-NY11.Rdata"
[1] "results/current-results-2012-2016-district-NY12.Rdata"
[1] "results/current-results-2012-2016-district-NY13.Rdata"
[1] "results/current-results-2012-2016-district-NY14.Rdata"
[1] "results/current-results-2012-2016-district-NY15.Rdata"
[1] "results/current-results-2012-2016-district-NY16.Rdata"
[1] "results/current-results-2012-2016-district-NY17.Rdata"
[1] "results/current-results-2012-2016-district-NY18.Rdata"
[1] "results/current-results-2012-2016-district-NY19.Rdata"
[1] "results/current-results-2012-2016-district-NY2.Rdata"
[1] "results/current-results-2012-2016-district-NY20.Rdata"
[1] "results/current-results-2012-2016-district-NY21.Rdata"
[1] "results/current-results-2012-2016-district-NY22.Rdata"
[1] "results/current-results-2012-2016-district-NY23.Rdata"
[1] "results/current-results-2012-2016-district-NY24.Rdata"
[1] "results/current-results-2012-2016-district-NY25.Rdata"
[1] "results/current-results-2012-2016-district-NY26.Rdata"
[1] "results/current-results-2012-2016-district-NY27.Rdata"
[1] "results/current-results-2012-2016-district-NY3.Rdata"
[1] "results/current-results-2012-2016-district-NY4.Rdata"
[1] "results/current-results-2012-2016-district-NY5.Rdata"
[1] "results/current-results-2012-2016-district-NY6.Rdata"
[1] "results/current-results-2012-2016-district-NY7.Rdata"
[1] "results/current-results-2012-2016-district-NY8.Rdata"
[1] "results/current-results-2012-2016-district-NY9.Rdata"
[1] "results/current-results-2016-2020-district-CA1.Rdata"
[1] "results/current-results-2016-2020-district-CA10.Rdata"
[1] "results/current-results-2016-2020-district-CA11.Rdata"
[1] "results/current-results-2016-2020-district-CA12.Rdata"
[1] "results/current-results-2016-2020-district-CA13.Rdata"
[1] "results/current-results-2016-2020-district-CA14.Rdata"
[1] "results/current-results-2016-2020-district-CA15.Rdata"
[1] "results/current-results-2016-2020-district-CA16.Rdata"
[1] "results/current-results-2016-2020-district-CA17.Rdata"
[1] "results/current-results-2016-2020-district-CA18.Rdata"
[1] "results/current-results-2016-2020-district-CA19.Rdata"
[1] "results/current-results-2016-2020-district-CA2.Rdata"
[1] "results/current-results-2016-2020-district-CA20.Rdata"
[1] "results/current-results-2016-2020-district-CA21.Rdata"
[1] "results/current-results-2016-2020-district-CA22.Rdata"
[1] "results/current-results-2016-2020-district-CA23.Rdata"
[1] "results/current-results-2016-2020-district-CA24.Rdata"
[1] "results/current-results-2016-2020-district-CA25.Rdata"
[1] "results/current-results-2016-2020-district-CA26.Rdata"
[1] "results/current-results-2016-2020-district-CA27.Rdata"
[1] "results/current-results-2016-2020-district-CA28.Rdata"
[1] "results/current-results-2016-2020-district-CA29.Rdata"
[1] "results/current-results-2016-2020-district-CA3.Rdata"
[1] "results/current-results-2016-2020-district-CA30.Rdata"
[1] "results/current-results-2016-2020-district-CA31.Rdata"
[1] "results/current-results-2016-2020-district-CA32.Rdata"
[1] "results/current-results-2016-2020-district-CA33.Rdata"
[1] "results/current-results-2016-2020-district-CA34.Rdata"
[1] "results/current-results-2016-2020-district-CA35.Rdata"
[1] "results/current-results-2016-2020-district-CA36.Rdata"
[1] "results/current-results-2016-2020-district-CA37.Rdata"
[1] "results/current-results-2016-2020-district-CA38.Rdata"
[1] "results/current-results-2016-2020-district-CA39.Rdata"
[1] "results/current-results-2016-2020-district-CA4.Rdata"
[1] "results/current-results-2016-2020-district-CA40.Rdata"
[1] "results/current-results-2016-2020-district-CA41.Rdata"
[1] "results/current-results-2016-2020-district-CA42.Rdata"
[1] "results/current-results-2016-2020-district-CA43.Rdata"
[1] "results/current-results-2016-2020-district-CA44.Rdata"
[1] "results/current-results-2016-2020-district-CA45.Rdata"
[1] "results/current-results-2016-2020-district-CA46.Rdata"
[1] "results/current-results-2016-2020-district-CA47.Rdata"
[1] "results/current-results-2016-2020-district-CA48.Rdata"
[1] "results/current-results-2016-2020-district-CA49.Rdata"
[1] "results/current-results-2016-2020-district-CA5.Rdata"
[1] "results/current-results-2016-2020-district-CA50.Rdata"
[1] "results/current-results-2016-2020-district-CA51.Rdata"
[1] "results/current-results-2016-2020-district-CA52.Rdata"
[1] "results/current-results-2016-2020-district-CA53.Rdata"
[1] "results/current-results-2016-2020-district-CA6.Rdata"
[1] "results/current-results-2016-2020-district-CA7.Rdata"
[1] "results/current-results-2016-2020-district-CA8.Rdata"
[1] "results/current-results-2016-2020-district-CA9.Rdata"
[1] "results/current-results-2016-2020-district-FL1.Rdata"
[1] "results/current-results-2016-2020-district-FL10.Rdata"
[1] "results/current-results-2016-2020-district-FL11.Rdata"
[1] "results/current-results-2016-2020-district-FL12.Rdata"
[1] "results/current-results-2016-2020-district-FL13.Rdata"
[1] "results/current-results-2016-2020-district-FL14.Rdata"
[1] "results/current-results-2016-2020-district-FL15.Rdata"
[1] "results/current-results-2016-2020-district-FL16.Rdata"
[1] "results/current-results-2016-2020-district-FL17.Rdata"
[1] "results/current-results-2016-2020-district-FL18.Rdata"
[1] "results/current-results-2016-2020-district-FL19.Rdata"
[1] "results/current-results-2016-2020-district-FL2.Rdata"
[1] "results/current-results-2016-2020-district-FL20.Rdata"
[1] "results/current-results-2016-2020-district-FL21.Rdata"
[1] "results/current-results-2016-2020-district-FL22.Rdata"
[1] "results/current-results-2016-2020-district-FL23.Rdata"
[1] "results/current-results-2016-2020-district-FL24.Rdata"
[1] "results/current-results-2016-2020-district-FL25.Rdata"
[1] "results/current-results-2016-2020-district-FL26.Rdata"
[1] "results/current-results-2016-2020-district-FL27.Rdata"
[1] "results/current-results-2016-2020-district-FL3.Rdata"
[1] "results/current-results-2016-2020-district-FL4.Rdata"
[1] "results/current-results-2016-2020-district-FL5.Rdata"
[1] "results/current-results-2016-2020-district-FL6.Rdata"
[1] "results/current-results-2016-2020-district-FL7.Rdata"
[1] "results/current-results-2016-2020-district-FL8.Rdata"
[1] "results/current-results-2016-2020-district-FL9.Rdata"
[1] "results/current-results-2016-2020-district-KS1.Rdata"
[1] "results/current-results-2016-2020-district-KS2.Rdata"
[1] "results/current-results-2016-2020-district-KS3.Rdata"
[1] "results/current-results-2016-2020-district-KS4.Rdata"
[1] "results/current-results-2016-2020-district-NC1.Rdata"
[1] "results/current-results-2016-2020-district-NC10.Rdata"
[1] "results/current-results-2016-2020-district-NC11.Rdata"
[1] "results/current-results-2016-2020-district-NC12.Rdata"
[1] "results/current-results-2016-2020-district-NC13.Rdata"
[1] "results/current-results-2016-2020-district-NC2.Rdata"
[1] "results/current-results-2016-2020-district-NC3.Rdata"
[1] "results/current-results-2016-2020-district-NC4.Rdata"
[1] "results/current-results-2016-2020-district-NC5.Rdata"
[1] "results/current-results-2016-2020-district-NC6.Rdata"
[1] "results/current-results-2016-2020-district-NC7.Rdata"
[1] "results/current-results-2016-2020-district-NC8.Rdata"
[1] "results/current-results-2016-2020-district-NC9.Rdata"
[1] "results/current-results-2016-2020-district-NY1.Rdata"
[1] "results/current-results-2016-2020-district-NY10.Rdata"
[1] "results/current-results-2016-2020-district-NY11.Rdata"
[1] "results/current-results-2016-2020-district-NY12.Rdata"
[1] "results/current-results-2016-2020-district-NY13.Rdata"
[1] "results/current-results-2016-2020-district-NY14.Rdata"
[1] "results/current-results-2016-2020-district-NY15.Rdata"
[1] "results/current-results-2016-2020-district-NY16.Rdata"
[1] "results/current-results-2016-2020-district-NY17.Rdata"
[1] "results/current-results-2016-2020-district-NY18.Rdata"
[1] "results/current-results-2016-2020-district-NY19.Rdata"
[1] "results/current-results-2016-2020-district-NY2.Rdata"
[1] "results/current-results-2016-2020-district-NY20.Rdata"
[1] "results/current-results-2016-2020-district-NY21.Rdata"
[1] "results/current-results-2016-2020-district-NY22.Rdata"
[1] "results/current-results-2016-2020-district-NY23.Rdata"
[1] "results/current-results-2016-2020-district-NY24.Rdata"
[1] "results/current-results-2016-2020-district-NY25.Rdata"
[1] "results/current-results-2016-2020-district-NY26.Rdata"
[1] "results/current-results-2016-2020-district-NY27.Rdata"
[1] "results/current-results-2016-2020-district-NY3.Rdata"
[1] "results/current-results-2016-2020-district-NY4.Rdata"
[1] "results/current-results-2016-2020-district-NY5.Rdata"
[1] "results/current-results-2016-2020-district-NY6.Rdata"
[1] "results/current-results-2016-2020-district-NY7.Rdata"
[1] "results/current-results-2016-2020-district-NY8.Rdata"
[1] "results/current-results-2016-2020-district-NY9.Rdata"

> print(mean(is.na(results$Estimate)))
[1] 0

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> d = fread('1976-2018-house.csv')[year%in%2012:2020 & state %in% c('California','North Carolina','New York','Florida','Kansas')]

> d[,pct:=candidatevotes/totalvotes]

> # calculate vote margin for each year and state district
> 
> # set order by year state district in descending order of pct of vote (so winner is top row)
> setorderv(d, c('year','state','district','pct'),c(1,-1,-1,-1))

> d = d[,list(candidate=.N,
+             margin = pct[1]-pct[2],
+             winner = party[1],
+             winning.pct=pct[1],
+             runnerup = party[2],
+             runerup.pct = pct[2]),by=c('year','state','district')]

> # create stacked ata for average margin between 2012-2016, and 2016-2020, where appropriate 
> d1 = d[year %in% 2012:2016][,list(
+   avg.margin=mean(margin,na.rm=T),
+   min.margin=min(margin,na.rm=T),
+   dem.control = mean(winner=='democrat',na.rm=T),
+   rep.control = mean(winner=='republican',na.rm=T),
+   n=.N,
+   nas=sum(is.na(margin))
+     
+     
+     
+   ),by=c('state','district')][,Years:='2012-2016']

> d2 = d[year %in% 2016:2020][,list(
+   avg.margin=mean(margin,na.rm=T),
+   min.margin=min(margin,na.rm=T),
+   dem.control = mean(winner=='democrat',na.rm=T),
+   rep.control = mean(winner=='republican',na.rm=T),
+   n=.N,
+   nas=sum(is.na(margin))
+   
+   
+   
+   
+ ),by=c('state','district')][,Years:='2016-2020']

> d = bind_rows(d1,d2)

> d[,district:=case_when(state=='California'~paste0('CA',district),
+                                 state=='New York'~paste0('NY',district),
+                                 state=='Kansas'~paste0('KS',district),
+                                 state=='North Carolina'~paste0('NC',district),
+                                 state=='Florida'~paste0('FL',district)
+ )]

> # merge to results, to classify results by competitive and uncompetive
> results = merge(results,d,all.x=T,by=c('Years','district'))

> # classify minimum margin as > .2 smallest margin and controlled by same party throught, competitive otherwise
> results[,Type := case_when(min.margin>.2 & (dem.control==1|rep.control==1)~'Uncompetitive',
+                           T ~ 'Competitive')]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> library(grid)

> g = ggplot(results, aes(x = Estimate, weight = N, fill = Subset, color = Subset, linetype = Type))+
+   geom_density(bw=.025,alpha=.2)+
+   theme_bw()+
+   geom_vline(xintercept = 0, linetype = 'dashed')+
+   facet_grid(Exposure.Type ~ Years + Subset)+
+   scale_color_manual(values=colors)+
+   scale_fill_manual(values=colors)+
+   xlab('District-level effect of partisan exposure')+
+   ylab('Density')+
+   theme(panel.spacing = unit(1, 'lines'))+
+   guides(color = 'none', fill = 'none')+
+   theme(legend.position = 'bottom',legend.title = element_blank(),
+         text = element_text(size=15, family='serif'), 
+         
+         strip.text.y = element_text(size=12,angle = 0))

> ggsave(plot = g, filename = 'figures/Fig8.png',dpi=300,width = 11, height  =4 ,units='in')

> toc()
3553.683 sec elapsed

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2748066   146.8   10863973   580.2   39197162  2093.4
Vcells 1937867141 14784.8 5939722894 45316.5 8366301193 63829.9

> # Table 1 - No code for Table 1 in repository -- Table does not use data and consists of a description of survey measures
> 
> # Table 2
> tic() # t .... [TRUNCATED] 

> source('EST-survey-analysis-weights.R', echo = T,  max.deparse.length=10000)

> rm(list = ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2748412   146.8    8691179   464.2   39197162  2093.4
Vcells 1937339813 14780.8 5939722894 45316.5 8366301193 63829.9

> library(data.table)

> library(dplyr)

> library(stringr)

> library(tidyr)

> library(gender)

> library(lfe)

> ## SURVEY PARTISAN SEGREGATION ANALYSIS WITH WEIGHTS
> 
> 
> 
> surv=fread('survey-analysis.csv.gz')

> surv[countyfips=='',countyfips:=NA]

> surv[ZipCode=='',ZipCode:=NA]

> surv[,Race:=factor(Race,levels=c('Other','Asian','Black','Hispanic','White'))]

> # Perceptions of neighborhoods
> 
> 
> m = felm(NeighborDems~ DemSpExp_nohh +hh.d.adj+hh.n.adj  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w1DemSpExp = summary(m,robust=T)

> w1DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(NeighborDems~ RepSpExp_nohh  +hh.r.adj+hh.n.adj + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w1RepSpExp = summary(m,robust=T)

> w1RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> #### Contact with neighbor dems
> 
> m = felm(ContactDemNeighbors~ DemSpExp_nohh +hh.d.adj+hh.n.adj  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w2DemSpExp = summary(m,robust=T)

> w2DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(ContactRepNeighbors~ RepSpExp_nohh +hh.r.adj+hh.n.adj + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w2RepSpExp = summary(m,robust=T)

> w2RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> # Comfort 
> 
> m = felm(Comfort~(DemSpExp_nohh + hh.d.adj+hh.n.adj+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Democrat |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w5DemSpExp = summary(m,robust=T)

> w5DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Comfort~(RepSpExp_nohh  + hh.r.adj+hh.n.adj+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Republican |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w5RepSpExp = summary(m,robust=T)

> w5RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> # Predicting party switching as function of comfort models
> dems = surv[Party_year1=='Democrat']

> reps = surv[Party_year1=='Republican']

> oths = surv[Party_year1=='Other']

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems, weights =  dems$w)

> w6DemSpExpDems = summary(m,robust=T)

> w6DemSpExpDems$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps, weights =  reps$w)

> w6DemSpExpReps = summary(m,robust=T)

> w6DemSpExpReps$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths, weights =  oths$w)

> w6DemSpExpOths = summary(m,robust=T)

> w6DemSpExpOths$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems, weights =  dems$w)

> w6RepSpExpDems = summary(m,robust=T)

> w6RepSpExpDems$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps, weights =  reps$w)

> w6RepSpExpReps = summary(m,robust=T)

> w6RepSpExpReps$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths, weights =  oths$w)

> w6RepSpExpOths = summary(m,robust=T)

> w6RepSpExpOths$mean.outcome=weighted.mean(m$response, w=m$weights)

> ##
> rm(surv,w,data,dems,reps,oths,m,t)

> save.image(file='results/survey-results-weights.Rdata')

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2748919   146.9    8691179   464.2   39197162  2093.4
Vcells 1937461258 14781.7 5939722894 45316.5 8366301193 63829.9

> source('EST-survey-analysis-no-weights.R', echo = T,  max.deparse.length=10000)

> rm(list = ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2749802   146.9    8691179   464.2   39197162  2093.4
Vcells 1937417107 14781.4 5939722894 45316.5 8366301193 63829.9

> library(data.table)

> library(dplyr)

> library(stringr)

> library(tidyr)

> library(gender)

> library(lfe)

> ## SURVEY PARTISAN SEGREGATION ANALYSIS WITHOUT WEIGHTS
> 
> surv=fread('survey-analysis.csv.gz')

> surv[countyfips=='',countyfips:=NA]

> surv[ZipCode=='',ZipCode:=NA]

> surv[,Race:=factor(Race,levels=c('Other','Asian','Black','Hispanic','White'))]

> # Perceptions of neighborhoods
> 
> 
> m = felm(NeighborDems~ DemSpExp_nohh +hh.d.adj+hh.n.adj  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m1DemSpExp = summary(m,robust=T)

> m1DemSpExp$mean.outcome=mean(m$response)

> m = felm(NeighborDems~ RepSpExp_nohh  +hh.r.adj+hh.n.adj + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m1RepSpExp = summary(m,robust=T)

> m1RepSpExp$mean.outcome=mean(m$response)

> #### Contact with neighbor dems
> 
> m = felm(ContactDemNeighbors~ DemSpExp_nohh +hh.d.adj+hh.n.adj  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m2DemSpExp = summary(m,robust=T)

> m2DemSpExp$mean.outcome=mean(m$response)

> m = felm(ContactRepNeighbors~ RepSpExp_nohh +hh.r.adj+hh.n.adj + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m2RepSpExp = summary(m,robust=T)

> m2RepSpExp$mean.outcome=mean(m$response)

> m = felm(Comfort~(DemSpExp_nohh + hh.d.adj+hh.n.adj+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Democrat |ZipCode|0|countyfips,data=surv)

> m5DemSpExp = summary(m,robust=T)

> m5DemSpExp$mean.outcome=mean(m$response)

> m = felm(Comfort~(RepSpExp_nohh  + hh.r.adj+hh.n.adj+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Republican |ZipCode|0|countyfips,data=surv)

> m5RepSpExp = summary(m,robust=T)

> m5RepSpExp$mean.outcome=mean(m$response)

> # Predicting party switching as function of comfort models
> dems = surv[Party_year1=='Democrat']

> reps = surv[Party_year1=='Republican']

> oths = surv[Party_year1=='Other']

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems)

> m6DemSpExpDems = summary(m,robust=T)

> m6DemSpExpDems$mean.outcome=(m$response)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps)

> m6DemSpExpReps = summary(m,robust=T)

> m6DemSpExpReps$mean.outcome=(m$response)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths)

> m6DemSpExpOths = summary(m,robust=T)

> m6DemSpExpOths$mean.outcome=(m$response)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems)

> m6RepSpExpDems = summary(m,robust=T)

> m6RepSpExpDems$mean.outcome=(m$response)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps)

> m6RepSpExpReps = summary(m,robust=T)

> m6RepSpExpReps$mean.outcome=(m$response)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths)

> m6RepSpExpOths = summary(m,robust=T)

> m6RepSpExpOths$mean.outcome=(m$response)

> ##
> rm(surv,w,data,dems,reps,oths,m,t)

> save.image(file='results/survey-results-no-weights.Rdata')

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2748972   146.9    8691179   464.2   39197162  2093.4
Vcells 1937540938 14782.3 5939722894 45316.5 8366301193 63829.9

> source('TAB-survey-results.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    2748940   146.9    8691179   464.2   39197162  2093.4
Vcells 1937488953 14781.9 5939722894 45316.5 8366301193 63829.9

> ## SURVEY RESULT TABLES
> library(modelsummary)

> library(dplyr)

> library(tidyr)

> library(stringr)

> library(tibble)

> library(readr)

> library(gt)

> library(data.table)

> library(gt)

> #gc()
> 
> load('results/survey-results-weights.Rdata')

> # Load custom function to make regression tables from summary.felm output
> source('felm-summary-table.R')

> means = 
+   as.data.frame(t(c("Mean Outcome", 
+     round(w1DemSpExp$mean.outcome,2),
+     round(w1RepSpExp$mean.outcome,2),
+     round(w2DemSpExp$mean.outcome,2),
+     round(w2RepSpExp$mean.outcome,2),
+     round(w5DemSpExp$mean.outcome,2),
+     round(w5RepSpExp$mean.outcome,2))))

> vars = c('Dem Exp', 'Dem Exp * Dem', 'Rep Exp' ,'Rep Exp * Rep')

> names(vars)= c('DemSpExp_nohh', 'DemSpExp_nohh:Democrat','RepSpExp_nohh','RepSpExp_nohh:Republican')

> l = list(w1DemSpExp, 
+          w1RepSpExp, 
+          w2DemSpExp, 
+          w2RepSpExp, 
+          w5DemSpExp, 
+          w5RepSpExp)

> names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)')

> tab1 = felm.summary.table(summaries = l,
+                           coef_map = vars, output ='gt',
+                           model.names=names(l), fmt =2,
+                           gof_map = c('nobs', 'r.squared', 'adj.r.squared'),
+                           add_rows = means)

> tab1 %>%
+ 
+   # column labels
+   tab_spanner(label = 'Neighbor PID', columns = 2:3) %>%
+   tab_spanner(label = 'Contact: Dems', columns = 4) %>%
+   tab_spanner(label = 'Contact: Reps', columns = 5) %>%
+   tab_spanner(label = 'Comfort: Neighbors know Party', columns = 6:7) %>%
+   
+   
+   as_latex()%>%
+   as.character %>%
+   str_replace_all('longtable','tabular')%>%
+   write_file('tables/Tab2.tex')

> toc()
24.145 sec elapsed

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3195203   170.7    8691179   464.2   39197162  2093.4
Vcells 1938745757 14791.5 5939722894 45316.5 8366301193 63829.9

> # Supporting Information Figures and Tables
> 
> # Table S1 - No code for Table S1 in repository -- Table does not use data and consists of a descri .... [TRUNCATED] 

> source('TAB-linked-unlinked-table.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3174804   169.6    8691179   464.2   39197162  2093.4
Vcells 1938237788 14787.6 5939722894 45316.5 8366301193 63829.9

> library(data.table)

> library(modelsummary)

> library(stringr)

> library(dplyr)

> library(xtable)

> library(tibble)

> library(tidyr)

> library(gt)

> library(readr)

> l=rbindlist(lapply(c(2016,2012,2008), FUN = function(y1){
+   
+ if(y1==2012){
+   y2=2016
+ } else if(y1==2016){
+   y2=2020
+ } else if(y1==2008){
+   y2=2012
+ }
+   s=fread(paste0('linked-unlinked-',y1,'-',y2,'.csv.gz'))
+   
+  
+   
+   s[,Female:=as.numeric(toupper(Gender)=='FEMALE')]
+   s[,White:=as.numeric(Race=='White')]
+   s[,Black:=as.numeric(Race=='Black')]
+   s[,Hispanic:=as.numeric(Race=='Hispanic')]
+   s[,Asian:=as.numeric(Race=='Asian')]
+ s[,Dem:=as.numeric(Party=='Democrat')]
+ s[,Rep:=as.numeric(Party=='Republican')]
+ 
+ 
+  means= s[,list(
+    Age = mean(Age_year1,na.rm=T),
+    
+       Democrat = mean(Dem,na.rm=T),
+       Republican = mean(Rep,na.rm=T),
+       White = mean(White,na.rm=T),
+       Black = mean(Black,na.rm=T),
+       Hispanic=mean(Hispanic,na.rm=T),
+       Asian = mean(Asian, na.rm=T),
+       Female = mean(Female,na.rm=T),
+      # `Vote General`= mean(GenVote,na.rm=T),
+     #  `Vote Primary`= mean(PrimVote,na.rm=T),
+       
+       `Block Group Democrat` = mean(DemBlockGroup,na.rm=T),
+       `Block Group Republican` = mean(RepBlockGroup,na.rm=T),
+       `Block Group White` = mean(WhiteBlockGroup,na.rm=T),
+       `Block Group Registered` = mean(RegBlockGroup,na.rm=T),
+       `Block Group Median Age`=mean(MedianAgeBlockGroup,na.rm=T),
+        `Block Group Median Household Income` = mean(HHIncomeBlockGroup,na.rm=T),
+       `Block Group Median Year House Built` = mean(YearBuiltBlockGroup,na.rm=T),
+       `Block Group Median House Value` = mean(HouseValueBlockGroup,na.rm=T),
+       `Block Group Homeowner` = mean(HomeownerBlockGroup,na.rm=T),
+        `Block Group Drive to Work` = mean(DriveWorkBlockGroup,na.rm=T),
+       `Democratic Exposure` = mean(DemSpExp_nohh_year1,na.rm=T),
+       `Republican Exposure` = mean(RepSpExp_nohh_year1,na.rm=T)
+ 
+       
+       
+       
+     ) ,by='Sample'][,Years:=paste0(y1,'-',y2)]
+     return(means)
+ }),fill=T)
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> ##  Table
> t = as_tibble(l)%>%
+   pivot_longer(Age:`Republican Exposure`)%>%
+   filter(Years=='2008-2012' & Sample=='Linked')%>%
+   mutate(Linked = value,
+          Variable = name)%>%
+ select(Variable,Linked)  %>%
+   left_join(
+     as_tibble(l)%>%
+       pivot_longer(Age:`Republican Exposure`)%>%
+       filter(Years=='2008-2012' & Sample=='Unlinked')%>%
+       mutate(Unlinked = value,
+              Variable = name)%>%
+   select(Variable,Unlinked)
+     
+     ) %>%
+   left_join( as_tibble(l)%>%
+                pivot_longer(Age:`Republican Exposure`)%>%
+                filter(Years=='2012-2016' & Sample=='Linked')%>%
+                mutate(Linked = value,
+                       Variable = name)%>%
+                select(Variable,Linked)  %>%
+                left_join(
+                  as_tibble(l)%>%
+                    pivot_longer(Age:`Republican Exposure`)%>%
+                    filter(Years=='2012-2016' & Sample=='Unlinked')%>%
+                    mutate(Unlinked = value,
+                           Variable = name)%>%
+                    select(Variable,Unlinked)
+                  
+                ),by='Variable')%>%
+   left_join( as_tibble(l)%>%
+                pivot_longer(Age:`Republican Exposure`)%>%
+                filter(Years=='2016-2020' & Sample=='Linked')%>%
+                mutate(Linked = value,
+                       Variable = name)%>%
+                select(Variable,Linked)  %>%
+                left_join(
+                  as_tibble(l)%>%
+                    pivot_longer(Age:`Republican Exposure`)%>%
+                    filter(Years=='2016-2020' & Sample=='Unlinked')%>%
+                    mutate(Unlinked = value,
+                           Variable = name)%>%
+                    select(Variable,Unlinked)
+                  
+                ),by='Variable')
Joining with `by = join_by(Variable)`
Joining with `by = join_by(Variable)`
Joining with `by = join_by(Variable)`

> out = t %>%
+   gt()%>%
+   fmt_number(columns = 2:7, decimals = 3)%>%
+   cols_label(
+     `Linked.x` = 'Linked',
+     `Unlinked.x` = 'Unlinked',
+     `Linked.y` = 'Linked',
+     `Unlinked.y` = 'Unlinked',
+     )%>%
+   tab_spanner(label = '2008-2012',columns = 2:3)%>%
+   tab_spanner(label = '2012-2016',columns = 4:5)%>%
+   tab_spanner(label = '2016-2020',columns = 6:7)%>%
+ 
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')

> write_file(out, 'tables/TabS2.tex')

> toc()
1249.137 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3186210   170.2    8691179   464.2   39197162  2093.4
Vcells 3389069646 25856.6 5939722894 45316.5 8366301193 63829.9

> # Table S3
> tic() # time scripts for each figure

> source('TAB-voteshare-correlation.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3186344   170.2    8691179   464.2   39197162  2093.4
Vcells 3389024041 25856.3 5939722894 45316.5 8366301193 63829.9

> library(dplyr)

> library(ggplot2)

> library(readr)

> library(tidyr)

> library(data.table)

> library(wCorr)
wCorr v1.9.8


> library(patchwork)

> library(xtable)

> prec = rbindlist(lapply(c(2012,2016,2020), FUN = function(x){
+   read_csv(paste0('precincts-',x,'.csv'))%>%
+     mutate(year=x)%>%
+     as.data.table
+ }),fill=T)%>%
+   drop_na()

[1mindexing[0m [34mprecincts-2012.csv[0m [=---------------------------] [32m1.44GB/s[0m, eta: [36m 0s[0m
[1mindexing[0m [34mprecincts-2012.csv[0m [==========================] [32m128.85MB/s[0m, eta: [36m 0s[0m
                                                                                                                
Rows: 22986 Columns: 8
── Column specification ─────────────────────────────────────────────────────
Delimiter: ","
chr (3): vf_source_state, vf_county_name, vf_precinct_name
dbl (5): dem.share, rep.share, p.dem, p.rep, n

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

[1mindexing[0m [34mprecincts-2016.csv[0m [=---------------------------] [32m1.48GB/s[0m, eta: [36m 0s[0m
[1mindexing[0m [34mprecincts-2016.csv[0m [==========================] [32m231.04MB/s[0m, eta: [36m 0s[0m
                                                                                                                
Rows: 26572 Columns: 8
── Column specification ─────────────────────────────────────────────────────
Delimiter: ","
chr (3): vf_source_state, vf_county_name, vf_precinct_name
dbl (5): dem.share, rep.share, p.dem, p.rep, n

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

[1mindexing[0m [34mprecincts-2020.csv[0m [----------------------------] [32m2.10GB/s[0m, eta: [36m 0s[0m
[1mindexing[0m [34mprecincts-2020.csv[0m [==========================] [32m142.74MB/s[0m, eta: [36m 0s[0m
                                                                                                                
Rows: 73193 Columns: 8
── Column specification ─────────────────────────────────────────────────────
Delimiter: ","
chr (3): vf_source_state, vf_county_name, vf_precinct_name
dbl (5): dem.share, rep.share, p.dem, p.rep, n

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

> prec.cor = prec%>%
+   group_by(year)%>%
+   summarize(d.cor = round(weightedCorr(x=p.dem,y=dem.share,weights=n, method='Pearson'),2),
+             r.cor  = round(weightedCorr(x=p.rep,y=rep.share,weights=n, method='Pearson'),2))

> cty =  rbindlist(lapply(c(2012,2016,2020), FUN = function(x){
+   read_csv(paste0('counties-',x,'.csv'))%>%
+     mutate(year=x)%>%
+     as.data.table
+ }),fill=T)%>%
+   drop_na()

[1mindexing[0m [34mcounties-2012.csv[0m [===========----------------] [32m424.06MB/s[0m, eta: [36m 0s[0m
[1mindexing[0m [34mcounties-2012.csv[0m [=============================] [32m3.81MB/s[0m, eta: [36m 0s[0m
                                                                                                                
Rows: 392 Columns: 7
── Column specification ─────────────────────────────────────────────────────
Delimiter: ","
dbl (7): county_fips, year, dem.share, rep.share, p.dem, p.rep, n

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

[1mindexing[0m [34mcounties-2016.csv[0m [====-----------------------] [32m175.90MB/s[0m, eta: [36m 0s[0m
[1mindexing[0m [34mcounties-2016.csv[0m [=============================] [32m3.84MB/s[0m, eta: [36m 0s[0m
                                                                                                                
Rows: 392 Columns: 7
── Column specification ─────────────────────────────────────────────────────
Delimiter: ","
dbl (7): county_fips, year, dem.share, rep.share, p.dem, p.rep, n

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

[1mindexing[0m [34mcounties-2020.csv[0m [==========-----------------] [32m300.61MB/s[0m, eta: [36m 0s[0m
[1mindexing[0m [34mcounties-2020.csv[0m [=============================] [32m3.24MB/s[0m, eta: [36m 0s[0m
                                                                                                                
Rows: 292 Columns: 7
── Column specification ─────────────────────────────────────────────────────
Delimiter: ","
dbl (7): county_fips, year, dem.share, rep.share, p.dem, p.rep, n

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

> cty.cor = cty%>%
+   group_by(year)%>%
+   summarize(d.cor = round(weightedCorr(x=p.dem,y=dem.share,weights=n, method='Pearson'),2),
+             r.cor  =round(weightedCorr(x=p.rep,y=rep.share,weights=n, method='Pearson'),2))

> cor = merge(prec.cor,cty.cor,by='year')

> print(xtable(cor),file = 'tables/TabS3.tex')

> toc()
6.154 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3297766   176.2    8691179   464.2   39197162  2093.4
Vcells 3389233413 25857.8 5939722894 45316.5 8366301193 63829.9

> # Table S4
> tic() # time scripts for each figure

> source('TAB-mover-neighborhoods.R', echo = T,  max.deparse.length=10000)

> # 19 - Descriptive tables
> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3297809   176.2    8691179   464.2   39197162  2093.4
Vcells 3389220822 25857.8 5939722894 45316.5 8366301193 63829.9

> library(data.table)

> library(modelsummary)

> library(stringr)

> library(dplyr)

> library(xtable)

> library(tibble)

> library(tidyr)

> library(gt)

> library(readr)

> data = fread('linked-unlinked-2012-2016.csv.gz',select=c('DemBlockGroup_year2', 'DemBlockGroup2_year2','RepBlockGroup','RepBlockGroup_year2','RepBlockGroup2_year2','Party_year2','Move'))[Move==1]
|--------------------------------------------------|
|==================================================|

> data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']

> t1=data[,list(
+ 
+           DemBlockGroup_year2_old = mean(DemBlockGroup_year2,na.rm=T),
+           DemBlockGroup_year2_new = mean(DemBlockGroup2_year2,na.rm=T),
+           
+  
+           RepBlockGroup_year2_old = mean(RepBlockGroup_year2,na.rm=T),
+           RepBlockGroup_year2_new = mean(RepBlockGroup2_year2,na.rm=T)
+           
+           
+           
+ ), by = 'Party_year2']

> data = fread('linked-unlinked-2016-2020.csv.gz',select=c('DemBlockGroup_year2', 'DemBlockGroup2_year2','RepBlockGroup','RepBlockGroup_year2','RepBlockGroup2_year2','Party_year2','Move'))[Move==1]
|--------------------------------------------------|
|==================================================|

> data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']

> t2 = data[,list(
+   
+   DemBlockGroup_year2_old = mean(DemBlockGroup_year2,na.rm=T),
+   DemBlockGroup_year2_new = mean(DemBlockGroup2_year2,na.rm=T),
+   
+   
+   RepBlockGroup_year2_old = mean(RepBlockGroup_year2,na.rm=T),
+   RepBlockGroup_year2_new = mean(RepBlockGroup2_year2,na.rm=T)
+   
+   
+   
+ ), by = 'Party_year2']

> # 
> t1[,Party:=Party_year2]

> t1[,Party_year2:=NULL]

> t2[,Party:=Party_year2]

> t2[,Party_year2:=NULL]

> t = merge(t1,t2,by='Party')

> out = t %>%
+   as_tibble %>%
+   gt()%>%
+   fmt_number(columns = 2:9, decimals = 3)%>%
+   cols_label(
+     `DemBlockGroup_year2_old.x` = 'Origin',
+     `DemBlockGroup_year2_new.x` = 'New',
+     `DemBlockGroup_year2_old.y` = 'Origin',
+     `DemBlockGroup_year2_new.y` = 'New',
+     
+     `RepBlockGroup_year2_old.x` = 'Origin',
+     `RepBlockGroup_year2_new.x` = 'New',
+     `RepBlockGroup_year2_old.y` = 'Origin',
+     `RepBlockGroup_year2_new.y` = 'New'
+   )%>%
+   tab_spanner(label = 'Prop. Democrat', 2:3)%>%
+   tab_spanner(label = 'Prop. Republican', 4:5)%>%
+   tab_spanner(label = 'Prop. DemocratREMOVE', 6:7)%>%
+   tab_spanner(label = 'Prop. RepublicanREMOVE', 8:9)%>%
+   tab_spanner(label = '2012-2016',columns = 2:5)%>%
+   tab_spanner(label = '2016-2020',columns = 6:9)%>%
+ 
+   
+   
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')%>%
+   str_replace_all('REMOVE','')

> write_file(out, 'tables/TabS4.tex')

> toc()
605.71 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3297614   176.2    8691179   464.2   39197162  2093.4
Vcells 3549964571 27084.1 5939722894 45316.5 8366301193 63829.9

> # Figure S1
> tic() # time scripts for each figure

> source('EST-model-moving-2012-2016.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells    3298714   176.2    8691179   464.2   39197162  2093.4
Vcells 3549917629 27083.8 5939722894 45316.5 8366301193 63829.9

> library(data.table)

> library(lfe)

> data = fread('linked-unlinked-2012-2016.csv.gz')[Sample=='Linked'|Move==1]
|--------------------------------------------------|
|==================================================|

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,MarriageDiff:=(Married_year2)-Married_year1]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> #format block group values
> 
> data[,DemBlockGroup_year1:=DemBlockGroup]

> data[,RepBlockGroup_year1:=RepBlockGroup]

> data[,DemBlockGroupDiff:=DemBlockGroup_year2-DemBlockGroup_year1]

> data[,RepBlockGroupDiff:=RepBlockGroup_year2-RepBlockGroup_year1]

> dems = data[Party=='Democrat']

> reps = data[Party=='Republican']

> oths = data[!Party%in%c('Democrat','Republican')]

> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # block treatment
> # democrats
> 
> # block group treatment
> # democrats
> dems[,DemBlockGroupDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemBlockGroupDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemBlockGroupDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republicans
> dems[,RepBlockGroupDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepBlockGroupDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepBlockGroupDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # block group treatment
> dems[,GroupDemBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, DemBlockGroupDecile,State,Married_year1,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupDemBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, DemBlockGroupDecile,State, Married_year1,hh.n.adj_year1,hh.d.adj_year1,sep ='_')]

> oths[,GroupDemBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, DemBlockGroupDecile,State,Married_year1, hh.n.adj_year1,hh.d.adj_year1,sep ='_')]

> dems[,GroupRepBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, RepBlockGroupDecile,State, Married_year1,hh.n.adj_year1,hh.r.adj_year1,sep ='_')]

> reps[,GroupRepBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, RepBlockGroupDecile,State, Married_year1,hh.n.adj_year1,hh.r.adj_year1,sep ='_')]

> oths[,GroupRepBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, RepBlockGroupDecile,State,Married_year1, hh.n.adj_year1,hh.r.adj_year1,sep ='_')]

> # blockgroup treatment
> ModelDemBlockGroupDems = summary(felm(Move ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff++WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupDemBlockGroup|0|countyfips, data = dems[!is.na(countyfips)& !is.infinite(DemBlockGroupDiff)&!is.na(DemBlockGroup_year1)] ), robust = T)

> ModelDemBlockGroupReps = summary(felm(Move ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupDemBlockGroup|0|countyfips, data = reps[!is.na(countyfips)& !is.infinite(DemBlockGroupDiff)&!is.na(DemBlockGroup_year1)] ), robust = T)

> ModelDemBlockGroupOths = summary(felm(Move ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupDemBlockGroup|0|countyfips, data = oths[!is.na(countyfips)& !is.infinite(DemBlockGroupDiff)&!is.na(DemBlockGroup_year1)] ), robust = T)

> ModelRepBlockGroupDems = summary(felm(Move ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupRepBlockGroup|0|countyfips, data = dems[!is.na(countyfips)& !is.infinite(RepBlockGroupDiff)&!is.na(RepBlockGroup_year1)] ), robust = T)

> ModelRepBlockGroupReps = summary(felm(Move ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupRepBlockGroup|0|countyfips, data = reps[!is.na(countyfips)& !is.infinite(RepBlockGroupDiff)&!is.na(RepBlockGroup_year1)] ), robust = T)

> ModelRepBlockGroupOths = summary(felm(Move ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupRepBlockGroup|0|countyfips, data = oths[!is.na(countyfips)& !is.infinite(RepBlockGroupDiff)&!is.na(RepBlockGroup_year1)] ), robust = T)

> save(ModelDemBlockGroupDems,ModelRepBlockGroupReps,ModelDemBlockGroupReps,ModelDemBlockGroupOths,
+      ModelRepBlockGroupDems, ModelRepBlockGroupOths,
+      
+      
+      
+      file = 'results/modeling-moving-2012-2016.Rdata'
+      
+ )

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3297329   176.1    28025154  1496.8    39197162  2093.4
Vcells 4302627870 32826.5 10317041402 78712.8 10315746981 78703.0

> source('EST-model-moving-2016-2020.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3298781   176.2    22420124  1197.4    39197162  2093.4
Vcells 4302629368 32826.5 10317041402 78712.8 10315746981 78703.0

> library(data.table)

> library(lfe)

> data = fread('linked-unlinked-2016-2020.csv.gz')[Sample=='Linked'|Move==1]
|--------------------------------------------------|
|==================================================|

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,MarriageDiff:=(Married_year2)-Married_year1]

> # make household composition
> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # format block group values
> data[,DemBlockGroup_year1:=DemBlockGroup]

> data[,RepBlockGroup_year1:=RepBlockGroup]

> data[,DemBlockGroupDiff:=DemBlockGroup_year2-DemBlockGroup_year1]

> data[,RepBlockGroupDiff:=RepBlockGroup_year2-RepBlockGroup_year1]

> # subset data
> dems = data[Party=='Democrat']

> reps = data[Party=='Republican']

> oths = data[!Party%in%c('Democrat','Republican')]

> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # block treatment
> # democrats
> 
> # block group treatment
> # democrats
> dems[,DemBlockGroupDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemBlockGroupDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemBlockGroupDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republicans
> dems[,RepBlockGroupDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepBlockGroupDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepBlockGroupDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # block group treatment
> dems[,GroupDemBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, DemBlockGroupDecile,State,Married_year1,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupDemBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, DemBlockGroupDecile,State, Married_year1,hh.n.adj_year1,hh.d.adj_year1,sep ='_')]

> oths[,GroupDemBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, DemBlockGroupDecile,State,Married_year1, hh.n.adj_year1,hh.d.adj_year1,sep ='_')]

> dems[,GroupRepBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, RepBlockGroupDecile,State, Married_year1,hh.n.adj_year1,hh.r.adj_year1,sep ='_')]

> reps[,GroupRepBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, RepBlockGroupDecile,State, Married_year1,hh.n.adj_year1,hh.r.adj_year1,sep ='_')]

> oths[,GroupRepBlockGroup:=paste(ZipCode,AgeDecile,Race, Gender, RepBlockGroupDecile,State,Married_year1, hh.n.adj_year1,hh.r.adj_year1,sep ='_')]

> # blockgroup treatment
> ModelDemBlockGroupDems = summary(felm(Move ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff++WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupDemBlockGroup|0|countyfips, data = dems[!is.na(countyfips)& !is.infinite(DemBlockGroupDiff)&!is.na(DemBlockGroup_year1)] ), robust = T)

> ModelDemBlockGroupReps = summary(felm(Move ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupDemBlockGroup|0|countyfips, data = reps[!is.na(countyfips)& !is.infinite(DemBlockGroupDiff)&!is.na(DemBlockGroup_year1)] ), robust = T)

> ModelDemBlockGroupOths = summary(felm(Move ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupDemBlockGroup|0|countyfips, data = oths[!is.na(countyfips)& !is.infinite(DemBlockGroupDiff)&!is.na(DemBlockGroup_year1)] ), robust = T)

> ModelRepBlockGroupDems = summary(felm(Move ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupRepBlockGroup|0|countyfips, data = dems[!is.na(countyfips)& !is.infinite(RepBlockGroupDiff)&!is.na(RepBlockGroup_year1)] ), robust = T)

> ModelRepBlockGroupReps = summary(felm(Move ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupRepBlockGroup|0|countyfips, data = reps[!is.na(countyfips)& !is.infinite(RepBlockGroupDiff)&!is.na(RepBlockGroup_year1)] ), robust = T)

> ModelRepBlockGroupOths = summary(felm(Move ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+MarriageDiff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                         HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                         DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupRepBlockGroup|0|countyfips, data = oths[!is.na(countyfips)& !is.infinite(RepBlockGroupDiff)&!is.na(RepBlockGroup_year1)] ), robust = T)

> save(ModelDemBlockGroupDems,ModelRepBlockGroupReps,ModelDemBlockGroupReps,ModelDemBlockGroupOths,
+      ModelRepBlockGroupDems, ModelRepBlockGroupOths,
+      
+      
+      
+      file = 'results/modeling-moving-2016-2020.Rdata'
+      
+ )

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3300148   176.3    30471601  1627.4    39197162  2093.4
Vcells 4489322269 34250.9 12380529682 94456.0 12379849752 94450.8

> source('FIG-modeling-moving.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3301060   176.3    24377281  1301.9    39197162  2093.4
Vcells 4489318686 34250.8 12380529682 94456.0 12379849752 94450.8

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c('modeling-moving-2012-2016.Rdata',
+                                         'modeling-moving-2016-2020.Rdata'
+   
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemBlockGroupDiff','RepBlockGroupDiff')]

> results[grepl('DemBlockGroup',Model),Exposure.Type:='Exposure\nto Democrats']

> results[grepl('RepBlockGroup',Model),Exposure.Type:='Exposure\nto Republicans']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g = ggplot(results, aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 24, family = 'serif'), legend.title=element_blank(),legend.position = 'bottom',panel.spacing = unit(1, "lines"))+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g,filename = 'figures/FigS1.png',dpi=300, width = 11, height = 6, units = 'in')

> toc()
3259 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3301032   176.3    19501825  1041.6    39197162  2093.4
Vcells 4489594608 34252.9 12380529682 94456.0 12379849752 94450.8

> # Table S5
> tic() # time scripts for each figure

> # If Figure 4 has not yet been created, will need to run the following commented out analysis scripts to create SD output
> # source('EST-current-re .... [TRUNCATED] 

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3303076   176.5    15601460   833.3    39197162  2093.4
Vcells 4489303334 34250.7 12380529682 94456.0 12379849752 94450.8

> library(data.table)

> library(modelsummary)

> library(stringr)

> library(dplyr)

> library(xtable)

> library(tibble)

> library(tidyr)

> library(gt)

> library(readr)

> load('results/sds/sds-2008-2012-main-nohh.Rdata')

> n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)

> n2 = sum(out[['Democrats - Rep Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)

> d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)

> d2 = sum(out[['Democrats - Rep Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

> n1_0812=n1

> n1/d1
[1] 0.7648227

> n2_0812=n2

> n2/d2
[1] 0.7663264

> dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Democrats']

> reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Republicans']

> oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Non-Partisans']

> p = rbind(dems,reps,oths)

> p1 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

> ##########
> 
> load('results/sds/sds-2012-2016-main-nohh.Rdata')

> n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)

> n2 = sum(out[['Democrats - Rep Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)

> d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)

> d2 = sum(out[['Democrats - Rep Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

> n1_1216=n1

> n1/d1
[1] 0.7325166

> n2_1216=n2

> n2/d2
[1] 0.7364627

> dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Democrats']

> reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Republicans']

> oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Non-Partisans']

> p = rbind(dems,reps,oths)

> p2 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

> ####
> 
> load('results/sds/sds-2016-2020-main-nohh.Rdata')

> n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)

> n2 = sum(out[['Democrats - Rep Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)

> d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)

> d2 = sum(out[['Democrats - Rep Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

> n1_1620=n1

> n1/d1
[1] 0.7740958

> n2_1620=n2

> n2/d2
[1] 0.7802747

> dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Democrats']

> reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Republicans']

> oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Non-Partisans']

> p = rbind(dems,reps,oths)

> p3 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

> ####
> 
> load("results/sds/sds-2012-2016-nohh-pretrend.Rdata")

> n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)

> n2 = sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)

> d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)

> d2 = sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

> n1
[1] 5850030

> n1/d1
[1] 0.8879047

> n2
[1] 5851722

> n2/d2
[1] 0.8881615

> dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Democrats']

> reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Republicans']

> oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Non-Partisans']

> p = rbind(dems,reps,oths)

> p4 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

> ##
> 
> 
> load('results/sds/sds-2016-2020-nohh-pretrend.Rdata')

> n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)

> n2 = sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)

> d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)

> d2 = sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

> n1
[1] 12796191

> n1/d1
[1] 0.81434

> n2
[1] 12884621

> n2/d2
[1] 0.8199677

> dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Democrats']

> reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Republicans']

> oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
+        out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
+        
+ )[,Subset:='Non-Partisans']

> p = rbind(dems,reps,oths)

> p5 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c('Exposure Type','Subset')]

> ###
> p1 = as_tibble(p1)%>%
+  mutate(sd=round(sd,3))

> p2 = as_tibble(p2)%>%
+  mutate(sd=round(sd,3))

> p3 = as_tibble(p3)%>%
+  mutate(sd=round(sd,3))

> p4 = as_tibble(p4)%>%
+  mutate(sd=round(sd,3))

> p5 = as_tibble(p5)%>%
+  mutate(sd=round(sd,3))

> p=p1 %>%
+  left_join(p2, by=c( 'Exposure Type','Subset'))%>%
+  left_join(p3, by=c( 'Exposure Type','Subset'))%>%
+  left_join(p4, by=c( 'Exposure Type','Subset'))%>%
+  left_join(p5, by=c( 'Exposure Type','Subset'))%>%
+  gt()%>%
+   fmt_number(columns = 3:7,
+              decimals = 3
+              )%>%
+   cols_label(
+     `sd.x` = '2008-2012',
+     `sd.y` = '2012-2016',
+     `sd.x.x` = '2016-2020',
+     `sd.y.y` = '2012-2016',
+     `sd` = '2016-2020'
+     )%>%
+  tab_spanner('Main Specification',3:5)%>%
+  tab_spanner('Pre-Trend Specification',6:7)%>%
+  as_latex()

> write_file(p, 'tables/TabS5.tex')

> toc()
244.895 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3300802   176.3    36147516  1930.5    45184394  2413.2
Vcells 4489448999 34251.8 12380529682 94456.0 12379849752 94450.8

> # Table S6
> tic() # time scripts for each figure

> source('TAB-party-covariate.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3301618   176.4    28918013  1544.4    45184394  2413.2
Vcells 4489291810 34250.6 12380529682 94456.0 12379849752 94450.8

> library(ggplot2)

> library(scales)

> library(data.table)

> library(stringr)

> library(dplyr)

> library(gt)

> library(tidyr)

> library(tibble)

> library(readr)

> files = c('panel-analysis-2008-2012.csv.gz',
+           'panel-analysis-2012-2016.csv.gz',
+           'panel-analysis-2016-2020.csv.gz')

> data = rbindlist(lapply(files, FUN = function(x){
+   d=fread(x, select = c(
+                         'Party_year1','Party_year2','DemDiff','RepDiff', 
+                         'DemSpExp_nohh_year1','RepSpExp_nohh_year1',
+                         'DemSpExpDiff_nohh', 'RepSpExpDiff_nohh', 
+                         'Race','Gender','Age_year1',
+                         'HomeownerBlockGroup_year1',
+                         'WhiteBlockGroup_year1',
+                         'HHIncomeBlockGroup_year1'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+   
+   return(d)
+ }),fill=T)
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> ### make party switching categories
> data[!Party_year1%in%c('Republican','Democrat'),Party_year1:='Non-Partisan']

> data[DemDiff==1|(DemDiff==0&Party_year1=='Democrat'),Party_year2:='Democrat']

> data[RepDiff==1|(RepDiff==0&Party_year1=='Republican'),Party_year2:='Republican']

> data[is.na(Party_year2),Party_year2:='Non-Partisan']

> data[Party_year1!='Democrat' & Party_year2=='Democrat',Status:='Switch Democrat']

> data[Party_year1=='Democrat' & Party_year2=='Democrat',Status:='Stable Democrat']

> data[Party_year1!='Republican' & Party_year2=='Republican',Status:='Switch Republican']

> data[Party_year1=='Republican' & Party_year2=='Republican',Status:='Stable Republican']

> data[Party_year1!='Non-Partisan' & Party_year2=='Non-Partisan',Status:='Switch Non-Partisan']

> data[Party_year1=='Non-Partisan' & Party_year2=='Non-Partisan',Status:='Stable Non-Partisan']

> ## Make table 
> 
> data[,Democrat:=as.numeric(Party_year1=='Democrat',na.rm=T)]

> data[,Republican:=as.numeric(Party_year1=='Republican',na.rm=T)]

> data[,Party_year1:=NULL]

> data[Gender=='',Gender:=NA]

> data[,Female:=as.numeric(Gender=='FEMALE')]

> data[,Gender:=NULL]

> data[,White:=as.numeric(Race=='White')]

> data[,Race:=NULL]

> data[Age_year1>120|Age_year1<18,Age_year1:=NA]

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3303671   176.5    23134411  1235.6    45184394  2413.2
Vcells 5669765154 43256.9 12380529682 94456.0 12380032691 94452.2

> p1=data[Years=='2008-2012']

> p2=data[Years=='2012-2016']

> p3=data[Years=='2016-2020']

> d1=p1[!is.na(Status),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                             White=mean(White,na.rm=T),
+                           `2008 Dem. Exp.`=mean(DemSpExp_nohh_year1,na.rm=T),
+                           `2008 Rep. Exp.`=mean(RepSpExp_nohh_year1,na.rm=T),
+                           `\\Delta Dem. Exp.`=mean(DemSpExpDiff_nohh,na.rm=T),
+                           `\\Delta Rep. Exp.`=mean(RepSpExpDiff_nohh,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+          
+         ),by='Status']

> ## p2
> 
> 
> 
> d2=p2[!is.na(Status),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                            # Democrat=mean(Democrat,na.rm=T),
+                           #  Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                           `2012 Dem. Exp.`=mean(DemSpExp_nohh_year1,na.rm=T),
+                           `2012 Rep. Exp.`=mean(RepSpExp_nohh_year1,na.rm=T),
+                           `\\Delta Dem. Exp.`=mean(DemSpExpDiff_nohh,na.rm=T),
+                           `\\Delta Rep. Exp.`=mean(RepSpExpDiff_nohh,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='Status']

> ## p3
> 
> 
> 
> d3=p3[!is.na(Status),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                           #  Democrat=mean(Democrat,na.rm=T),
+                           #  Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                           `2016 Dem. Exp.`=mean(DemSpExp_nohh_year1,na.rm=T),
+                           `2016 Rep. Exp.`=mean(RepSpExp_nohh_year1,na.rm=T),
+                           `\\Delta Dem. Exp.`=mean(DemSpExpDiff_nohh,na.rm=T),
+                           `\\Delta Rep. Exp.`=mean(RepSpExpDiff_nohh,na.rm=T),
+                         
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='Status']

> ###
> t = d1 %>%
+   as_tibble %>%
+   pivot_longer(Age:`Block Group Homeowner`)%>%
+   mutate(Sample = '2008-2012')%>%
+   pivot_wider(names_from = 'Status')%>%
+   select(Years=Sample, Variable = name,`Stable Democrat`, `Switch Democrat`, `Stable Republican`, `Switch Republican`, `Stable Non-Partisan`, `Switch Non-Partisan`)%>%
+   bind_rows(d2 %>%
+               as_tibble %>%
+               pivot_longer(Age:`Block Group Homeowner`)%>%
+               mutate(Sample = '2012-2016')%>%
+               pivot_wider(names_from = 'Status')%>%
+               select(Years=Sample, Variable = name,`Stable Democrat`, `Switch Democrat`, `Stable Republican`, `Switch Republican`, `Stable Non-Partisan`, `Switch Non-Partisan`))%>%
+   bind_rows(d3 %>%
+               as_tibble %>%
+               pivot_longer(Age:`Block Group Homeowner`)%>%
+               mutate(Sample = '2016-2020')%>%
+               pivot_wider(names_from = 'Status')%>%
+               select(Years=Sample, Variable = name,`Stable Democrat`, `Switch Democrat`, `Stable Republican`, `Switch Republican`, `Stable Non-Partisan`, `Switch Non-Partisan`))

>             out = t %>%
+               gt()%>%
+               fmt_number(columns = 3:8, decimals = 3)%>%
+           
+               
+               
+               
+               as_latex()%>%
+               as.character()%>%
+               str_replace_all('longtable','tabular')

>             write_file(out, 'tables/TabS6.tex')

> toc()
2886.499 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3305959   176.6    18507529   988.5    45184394  2413.2
Vcells 4630470649 35327.7 12380529682 94456.0 12380032691 94452.2

> # Table S7
> tic() # time scripts for each figure

> source('TAB-seg-covariate.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3307169   176.7    14806024   790.8    45184394  2413.2
Vcells 4630426411 35327.4 12380529682 94456.0 12380032691 94452.2

> library(ggplot2)

> library(scales)

> library(data.table)

> library(stringr)

> library(dplyr)

> library(tibble)

> library(tidyr)

> library(gt)

> library(readr)

> files = c('panel-analysis-2008-2012.csv.gz',
+           'panel-analysis-2012-2016.csv.gz',
+           'panel-analysis-2016-2020.csv.gz')

> data = rbindlist(lapply(files, FUN = function(x){
+   d=fread(x, select = c(
+                         'Party_year1', 'DemSpExpDiff_nohh', 'RepSpExpDiff_nohh', 
+                         'Race','Gender','Age_year1',
+                         'HomeownerBlockGroup_year1',
+                         'WhiteBlockGroup_year1',
+                         'HHIncomeBlockGroup_year1'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+   
+   return(d)
+ }),fill=T)
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> ### make party switching categories
> data[DemSpExpDiff_nohh >= (-.05) & DemSpExpDiff_nohh<= (.05),DemTreat:='D:[-0.05, 0.05]']

> data[ DemSpExpDiff_nohh<(-.05),DemTreat:='D:<-0.05']

> data[ DemSpExpDiff_nohh> (.05),DemTreat:='D:>0.05']

> data[RepSpExpDiff_nohh >= (-.05) & RepSpExpDiff_nohh<=(.05),RepTreat:='R:[-0.05, 0.05]']

> data[ RepSpExpDiff_nohh<(-.05),RepTreat:='R:<-0.05']

> data[ RepSpExpDiff_nohh> (.05),RepTreat:='R:>0.05']

> ## Make table 
> 
> data[,Democrat:=as.numeric(Party_year1=='Democrat',na.rm=T)]

> data[,Republican:=as.numeric(Party_year1=='Republican',na.rm=T)]

> data[,Party_year1:=NULL]

> data[Gender=='',Gender:=NA]

> data[,Female:=as.numeric(Gender=='FEMALE')]

> data[,Gender:=NULL]

> data[,DemSpExpDiff_nohh:=NULL]

> data[,RepSpExpDiff_nohh:=NULL]

> data[,White:=as.numeric(Race=='White')]

> data[,Race:=NULL]

> data[Age_year1>120|Age_year1<18,Age_year1:=NA]

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3307679   176.7    11844820   632.6    45184394  2413.2
Vcells 5425962343 41396.9 12380529682 94456.0 12380032691 94452.2

> p1=data[Years=='2008-2012']

> p2=data[Years=='2012-2016']

> p3=data[Years=='2016-2020']

> d1=p1[!is.na(DemTreat),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+          Democrat=mean(Democrat,na.rm=T),
+          Republican=mean(Republican,na.rm=T),
+          White=mean(White,na.rm=T),
+          `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+          `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+          `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+          
+          
+         ),by='DemTreat']

> r1=p1[!is.na(RepTreat),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                             Democrat=mean(Democrat,na.rm=T),
+                             Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='RepTreat']

> ## p2
> 
> 
> 
> d2=p2[!is.na(DemTreat),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                             Democrat=mean(Democrat,na.rm=T),
+                             Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='DemTreat']

> r2=p2[!is.na(RepTreat),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                             Democrat=mean(Democrat,na.rm=T),
+                             Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='RepTreat']

> ## p3
> 
> 
> 
> d3=p3[!is.na(DemTreat),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                             Democrat=mean(Democrat,na.rm=T),
+                             Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='DemTreat']

> r3=p3[!is.na(RepTreat),list(Age=round(mean(Age_year1,na.rm=T)),
+                             Female=mean(Female,na.rm=T),
+                             Democrat=mean(Democrat,na.rm=T),
+                             Republican=mean(Republican,na.rm=T),
+                             White=mean(White,na.rm=T),
+                             `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
+                             `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
+                             `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
+                             
+                             
+                             
+ ),by='RepTreat']

> ###
> 
> 
> 
> t = d1 %>%
+   as_tibble %>%
+   pivot_longer(Age:`Block Group Homeowner`)%>%
+   mutate(Sample = '2008-2012')%>%
+   pivot_wider(names_from = 'DemTreat')%>%
+   select(Sample, Variable=name,`D:<-0.05`,`D:[-0.05, 0.05]`,`D:>0.05`)%>%
+   bind_rows(d2 %>%
+               as_tibble %>%
+               pivot_longer(Age:`Block Group Homeowner`)%>%
+               mutate(Sample = '2012-2016')%>%
+               pivot_wider(names_from = 'DemTreat')%>%
+               select(Sample, Variable=name,`D:<-0.05`,`D:[-0.05, 0.05]`,`D:>0.05`))%>%
+   bind_rows(d3 %>%
+               as_tibble %>%
+               pivot_longer(Age:`Block Group Homeowner`)%>%
+               mutate(Sample = '2016-2020')%>%
+               pivot_wider(names_from = 'DemTreat')%>%
+               select(Sample, Variable=name,`D:<-0.05`,`D:[-0.05, 0.05]`,`D:>0.05`))%>%
+   left_join(
+     r1 %>%
+       as_tibble %>%
+       pivot_longer(Age:`Block Group Homeowner`)%>%
+       mutate(Sample = '2008-2012')%>%
+       pivot_wider(names_from = 'RepTreat')%>%
+       select(Sample, Variable=name,`R:<-0.05`,`R:[-0.05, 0.05]`,`R:>0.05`)%>%
+       bind_rows(r2 %>%
+                   as_tibble %>%
+                   pivot_longer(Age:`Block Group Homeowner`)%>%
+                   mutate(Sample = '2012-2016')%>%
+                   pivot_wider(names_from = 'RepTreat')%>%
+                   select(Sample, Variable=name,`R:<-0.05`,`R:[-0.05, 0.05]`,`R:>0.05`))%>%
+       bind_rows(r3 %>%
+                   as_tibble %>%
+                   pivot_longer(Age:`Block Group Homeowner`)%>%
+                   mutate(Sample = '2016-2020')%>%
+                   pivot_wider(names_from = 'RepTreat')%>%
+                   select(Sample, Variable=name,`R:<-0.05`,`R:[-0.05, 0.05]`,`R:>0.05`))
+     ,by=c('Sample','Variable'))

> out = t %>%
+   gt()%>%
+   fmt_number(columns = 3:8, decimals = 3)%>%
+   cols_label(
+     `D:>0.05` = '> 0.05',
+     `D:[-0.05, 0.05]` = '[-0.05, 0.05]',
+     `D:<-0.05` = '< -0.05',
+     `R:>0.05` = '> 0.05',
+     `R:[-0.05, 0.05]` = '[-0.05, 0.05]',
+     `R:<-0.05` = '< -0.05',
+     `Variable` = 'Variable'
+   )%>%
+   tab_spanner(label = '\\Delta Dem. Exp.', 3:5)%>%
+   tab_spanner(label = '\\Delta Rep. Exp.', 6:8)%>%
+   
+   
+   
+   
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')

> write_file(out, 'tables/TabS7.tex')

> toc()
2520.441 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3306283   176.6     9475856   506.1    45184394  2413.2
Vcells 4733135613 36111.0 12380529682 94456.0 12380032691 94452.2

> # Table S8 
> tic() # time scripts for each figure

> source('TAB-current-effects-full.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3306715   176.6     9475856   506.1    45184394  2413.2
Vcells 4733056794 36110.4 12380529682 94456.0 12380032691 94452.2

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(tidyr)

> library(tibble)

> library(stringr)

> library(gt)

> library(readr)

> library(modelsummary)

> source('felm-summary-table.R')

> files = paste0('results/',c('current-results-2008-2012.Rdata',
+                                         'current-results-2012-2016.Rdata',
+                                         'current-results-2016-2020.Rdata'))

> load(files[1])

> ModelDemSpExpReps1=ModelDemSpExpReps

> ModelDemSpExpOths1=ModelDemSpExpOths

> ModelDemSpExpDems1=ModelDemSpExpDems

> ModelRepSpExpReps1=ModelRepSpExpReps

> ModelRepSpExpOths1=ModelRepSpExpOths

> ModelRepSpExpDems1=ModelRepSpExpDems

> load(files[2])

> ModelDemSpExpReps2=ModelDemSpExpReps

> ModelDemSpExpOths2=ModelDemSpExpOths

> ModelDemSpExpDems2=ModelDemSpExpDems

> ModelRepSpExpReps2=ModelRepSpExpReps

> ModelRepSpExpOths2=ModelRepSpExpOths

> ModelRepSpExpDems2=ModelRepSpExpDems

> load(files[3])

> ModelDemSpExpReps3=ModelDemSpExpReps

> ModelDemSpExpOths3=ModelDemSpExpOths

> ModelDemSpExpDems3=ModelDemSpExpDems

> ModelRepSpExpReps3=ModelRepSpExpReps

> ModelRepSpExpOths3=ModelRepSpExpOths

> ModelRepSpExpDems3=ModelRepSpExpDems

> varlist1 = c('DemSpExpDiff_nohh' = '$\\Delta$ Dem Exp',
+   'hh.d.diff' = '$\\Delta$ HH Democrats',
+   'hh.n.diff' = '$\\Delta$ HH Voters',
+   'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
+   'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
+   'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
+   'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
+   'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
+   'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
+   'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
+   'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
+   'MarriageDiff' = '$\\Delta$ Married',
+   'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
+   'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
+   
+   )

> varlist2 = c('RepSpExpDiff_nohh' = '$\\Delta$ Rep Exp',
+              'hh.r.diff' = '$\\Delta$ HH Republicans',
+              'hh.n.diff' = '$\\Delta$ HH Voters',
+              'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
+              'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
+              'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
+              'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
+              'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
+              'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
+              'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
+              'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
+              'MarriageDiff' = '$\\Delta$ Married',
+              'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
+              'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
+              
+ )

> dt = felm.summary.table(summaries = list(ModelDemSpExpReps1, ModelDemSpExpOths1, ModelDemSpExpDems1, ModelDemSpExpReps2, ModelDemSpExpOths2, ModelDemSpExpDems2, ModelDemSpExpReps3, ModelDemSpExpOths3, ModelDemSpExpDems3),
+                          output='latex', fmt=3,
+                         coef_map = varlist1)

> rt = felm.summary.table(summaries = list(ModelRepSpExpReps1, ModelRepSpExpOths1, ModelRepSpExpDems1, ModelRepSpExpReps2, ModelRepSpExpOths2, ModelRepSpExpDems2, ModelRepSpExpReps3, ModelRepSpExpOths3, ModelRepSpExpDems3),
+                          output='latex', fmt=3,
+                         coef_map = varlist2)

> write_file(dt, 'tables/TabS8a.tex')

> write_file(rt, 'tables/TabS8b.tex')

> toc()
1665.805 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3325327   177.6    68856710  3677.4    58645897  3132.1
Vcells 4733118921 36110.9 12380529682 94456.0 12380032691 94452.2

> # Table S9 (and S11)
> tic() # time scripts for each figure

> source('TAB-effect-size.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3328127   177.8    55085368  2941.9    58645897  3132.1
Vcells 4733121848 36110.9 12380529682 94456.0 12380032691 94452.2

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> ##### get party switching rates
> 
> 
> data = rbindlist(lapply(c('panel-analysis-2008-2012.csv.gz',
+                           'panel-analysis-2012-2016.csv.gz',
+                           'panel-analysis-2016-2020.csv.gz'), FUN = function(x){
+                             d=fread(x, select = c('Party_year1','DemDiff','RepDiff'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+                             
+                             
+                             return(d)
+                           }))
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'),Party_year1:='Non-Partisan']

> data[DemDiff==1|(Party_year1=='Democrat' & DemDiff==0),Party_year2:='Democrat']

> data[RepDiff==1|(Party_year1=='Republican' & RepDiff==0),Party_year2:='Republican']

> data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']

> data[,DemDiff:=NULL]

> data[,RepDiff:=NULL]

> data[,Year1:=substr(Years,1,4)]

> data[,Year2:=substr(Years,6,9)]

> d = data[Years=='2016-2020']

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> library(dplyr)

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Years:='2016-2020']

> p1620=p

> ## 2012-2016
> 
> d = data[Years=='2012-2016']

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3328580   177.8    44068295  2353.6    58645897  3132.1
Vcells 5218124446 39811.2 12380529682 94456.0 12380032691 94452.2

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Years:='2012-2016']

> p1216=p

> ## 2008 - 2012 
> d = data[Years=='2008-2012']

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Years:='2008-2012']

> p0812=p

> ##### current results table
> 
> files = paste0('results/',c('current-results-2008-2012.Rdata',
+                                         'current-results-2012-2016.Rdata',
+                                         'current-results-2016-2020.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+   
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+     # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     
+     
+     
+     return(out)
+     
+   }))
+   
+   l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
+                       grepl('2012-2016',file)~ '2012-2016',
+                       grepl('2016-2020',file)~ '2016-2020')]  
+   l[,file:=file]
+   
+   
+   return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('2008-2012',file),Years:='2008-2012']

> results[grepl('2012-2016',file),Years:='2012-2016']

> results[grepl('2016-2020',file),Years:='2016-2020']

> results[grepl('ExpDems',Model),Subset:='Democrat']

> results[grepl('ExpReps',Model),Subset:='Republican']

> results[grepl('ExpOths',Model),Subset:='Non-Partisan']

> results[grepl('DemSp',Model),Outcome:='Democrat']

> results[grepl('RepSp',Model),Outcome:='Republican']

> p=rbind(p0812,p1216,p1620,fill=T)

> p[,Subset:=Party_year1]

> p[,Outcome:=Party_year2]

> t = merge(results[,c('Estimate', 'Years','Subset','Outcome')],
+           p[,c('prob', 'Years','Subset','Outcome')])

> t[Outcome=='Democrat',Exposure := 'Democratic']

> t[Outcome=='Republican',Exposure:= 'Republican']

> t[,`Starting\nparty`:=Subset]

> t[,`Effect of\n10 p.p.\nincrease`:=Estimate/10]

> t[,`Baseline\nprobability\nof outcome`:=prob]

> t[,`Percentage\nincrease`:=paste0(round((`Effect of\n10 p.p.\nincrease`/`Baseline\nprobability\nof outcome`)*100,1),'%')]

> t = t[,c('Outcome', 'Years', 'Exposure', 'Starting\nparty', 
+          'Effect of\n10 p.p.\nincrease', 'Baseline\nprobability\nof outcome',
+          'Percentage\nincrease')]

> library(xtable)

> t = t[order(`Years`, Exposure, `Starting\nparty`)]

> print(xtable(t, digits = 4), file = 'tables/TabS9.tex')

> rm(t,results)

> # downstream effect
> 
> 
> files = paste0('results/',c('future-results-2008-2012.Rdata',
+                                         'future-results-2012-2016.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+   
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+     # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     
+     
+     
+     return(out)
+     
+   }))
+   
+   l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
+                       grepl('2012-2016',file)~ '2012-2016',
+                       grepl('2016-2020',file)~ '2016-2020')]  
+   l[,file:=file]
+   
+   
+   return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('2008-2012',file),`Treatment Years`:='2008-2012']

> results[grepl('2012-2016',file),`Treatment Years`:='2012-2016']

> results[grepl('2008-2012',file),`Years`:='2012-2016']

> results[grepl('2012-2016',file),`Years`:='2016-2020']

> results[grepl('ExpDems',Model),Subset:='Democrat']

> results[grepl('ExpReps',Model),Subset:='Republican']

> results[grepl('ExpOths',Model),Subset:='Non-Partisan']

> results[grepl('DemSp',Model),Outcome:='Democrat']

> results[grepl('RepSp',Model),Outcome:='Republican']

> t = merge(results[,c('Estimate','Treatment Years', 'Years','Subset','Outcome')],
+           p[,c('prob', 'Years','Subset','Outcome')])

> t[Outcome=='Democrat',Exposure := 'Democratic']

> t[Outcome=='Republican',Exposure:= 'Republican']

> t[,`Outcome Years`:=Years]

> t[,`Starting\nparty`:=Subset]

> t[,`Effect of\n10 p.p.\nincrease`:=Estimate/10]

> t[,`Baseline\nprobability\nof outcome`:=prob]

> t[,`Percentage\nincrease`:=paste0(round((`Effect of\n10 p.p.\nincrease`/`Baseline\nprobability\nof outcome`)*100,1),'%')]

> t = t[,c('Outcome', 'Treatment Years', 'Outcome Years','Exposure', 'Starting\nparty', 
+          'Effect of\n10 p.p.\nincrease', 'Baseline\nprobability\nof outcome',
+          'Percentage\nincrease')]

> library(xtable)

> t = t[order(`Treatment Years`, Exposure, `Starting\nparty`)]

> print(xtable(t, digits = 4), file = 'tables/TabS11.tex')

> toc()
1035.21 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3326517   177.7    28203709  1506.3    58645897  3132.1
Vcells 4792520309 36564.1 12380529682 94456.0 12380032691 94452.2

> # Table S10 
> tic() # time scripts for each figure

> source('TAB-future-effects-full.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger  (Mb)    max used    (Mb)
Ncells    3326614   177.7    22562968  1205    58645897  3132.1
Vcells 4792336888 36562.7 12380529682 94456 12380032691 94452.2

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(tidyr)

> library(tibble)

> library(readr)

> library(stringr)

> library(gt)

> library(modelsummary)

> source('felm-summary-table.R')

> files = paste0('results/',c('future-results-2008-2012.Rdata',
+                                         'future-results-2012-2016.Rdata'))

> load(files[1])

> ModelDemSpExpReps1=ModelDemSpExpReps

> ModelDemSpExpOths1=ModelDemSpExpOths

> ModelDemSpExpDems1=ModelDemSpExpDems

> ModelRepSpExpReps1=ModelRepSpExpReps

> ModelRepSpExpOths1=ModelRepSpExpOths

> ModelRepSpExpDems1=ModelRepSpExpDems

> load(files[2])

> ModelDemSpExpReps2=ModelDemSpExpReps

> ModelDemSpExpOths2=ModelDemSpExpOths

> ModelDemSpExpDems2=ModelDemSpExpDems

> ModelRepSpExpReps2=ModelRepSpExpReps

> ModelRepSpExpOths2=ModelRepSpExpOths

> ModelRepSpExpDems2=ModelRepSpExpDems

> varlist1 = c('DemSpExpDiff_nohh' = '$\\Delta$ Dem Exp',
+              'hh.d.diff' = '$\\Delta$ HH Democrats',
+              'hh.n.diff' = '$\\Delta$ HH Voters',
+              'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
+              'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
+              'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
+              'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
+              'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
+              'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
+              'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
+              'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
+              'MarriageDiff' = '$\\Delta$ Married',
+              'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
+              'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
+              
+ )

> varlist2 = c('RepSpExpDiff_nohh' = '$\\Delta$ Rep Exp',
+              'hh.r.diff' = '$\\Delta$ HH Republicans',
+              'hh.n.diff' = '$\\Delta$ HH Voters',
+              'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
+              'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
+              'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
+              'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
+              'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
+              'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
+              'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
+              'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
+              'MarriageDiff' = '$\\Delta$ Married',
+              'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
+              'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
+              
+ )

> dt = felm.summary.table(summaries = list(ModelDemSpExpReps1, ModelDemSpExpOths1, ModelDemSpExpDems1, ModelDemSpExpReps2, ModelDemSpExpOths2, ModelDemSpExpDems2),
+                         output='latex',fmt=3,
+                         coef_map=varlist1)

> rt = felm.summary.table(summaries = list(ModelRepSpExpReps1, ModelRepSpExpOths1, ModelRepSpExpDems1, ModelRepSpExpReps2, ModelRepSpExpOths2, ModelRepSpExpDems2),
+                         output='latex', fmt = 3,
+                         coef_map=varlist2)

> write_file(dt, 'tables/TabS10a.tex')

> write_file(rt, 'tables/TabS10b.tex')

> toc()
421.311 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3327397   177.8    31307526  1672.1    58645897  3132.1
Vcells 4792354475 36562.8 12380529682 94456.0 12380158516 94453.2

> # Table S11 created previously(otherwise run the commented out scripts)
> #source('TAB-effect-size.R', echo = T,  max.deparse.length=10000)
> 
> 
>  .... [TRUNCATED] 

> source('EST-current-results-2016-2020-dwelling-age-white.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3329112   177.8    25046021  1337.7    58645897  3132.1
Vcells 4792358109 36562.8 12380529682 94456.0 12380158516 94453.2

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'vb.tsmart_dwelling_type'))
|--------------------------------------------------|
|==================================================|

> data = data[Race=='White']

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,Age_strata := ifelse(Age_year1 <= 34, '18-34', 
+                            ifelse(Age_year1 %in% 35:49, '35-49',
+                                   ifelse(Age_year1 %in% 50:64, '50-64', '65+')))]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>  # rm(data)
>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3337873   178.3    20036817  1070.1    58645897  3132.1
Vcells 6006508022 45826.1 12380529682 94456.0 12380158516 94453.2

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems1=dems

> reps1=reps

> oths1=oths

> for(i in c('S','H')){
+   for(j in unique(data$Age_strata)[!is.na(unique(data$Age_strata))]){
+     dems=dems1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     reps=reps1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     oths=oths1[vb.tsmart_dwelling_type==i & Age_strata==j]
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-',i,'-',j,'-white.Rdata'))
+   }
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3327990   177.8    16029454   856.1    58645897  3132.1
Vcells 4906991088 37437.4 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-dwelling-age-white.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3329120   177.8    12823564   684.9    58645897  3132.1
Vcells 4906752836 37435.6 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'vb.tsmart_dwelling_type'))
|--------------------------------------------------|
|==================================================|

> data = data[Race=='White']

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,Age_strata := ifelse(Age_year1 <= 34, '18-34', 
+                            ifelse(Age_year1 %in% 35:49, '35-49',
+                                   ifelse(Age_year1 %in% 50:64, '50-64', '65+')))]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>  # rm(data)
>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3337870   178.3    10258852   547.9    58645897  3132.1
Vcells 5896593573 44987.5 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems1=dems

> reps1=reps

> oths1=oths

> for(i in c('S','H')){
+   for(j in unique(data$Age_strata)[!is.na(unique(data$Age_strata))]){
+     dems=dems1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     reps=reps1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     oths=oths1[vb.tsmart_dwelling_type==i & Age_strata==j]
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-',i,'-',j,'-white.Rdata'))
+   }
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3327420   177.8    17189980   918.1    58645897  3132.1
Vcells 5009179356 38217.1 12380529682 94456.0 12380342215 94454.6

> source('FIG-current-results-age-dwelling-whites.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3328638   177.8    13751984   734.5    58645897  3132.1
Vcells 5009168845 38217.0 12380529682 94456.0 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c("current-results-2012-2016-S-18-34-white.Rdata",
+                                         "current-results-2012-2016-S-35-49-white.Rdata",
+                                         "current-results-2012-2016-S-50-64-white.Rdata",
+                                         "current-results-2012-2016-S-65+-white.Rdata",
+                                         
+                                         "current-results-2012-2016-H-18-34-white.Rdata",
+                                         "current-results-2012-2016-H-35-49-white.Rdata",
+                                         "current-results-2012-2016-H-50-64-white.Rdata",
+                                         "current-results-2012-2016-H-65+-white.Rdata",
+   
+   
+                                         "current-results-2016-2020-S-18-34-white.Rdata",
+                                         "current-results-2016-2020-S-35-49-white.Rdata",
+                                         "current-results-2016-2020-S-50-64-white.Rdata",
+                                         "current-results-2016-2020-S-65+-white.Rdata",
+                                         
+                                         "current-results-2016-2020-H-18-34-white.Rdata",
+                                         "current-results-2016-2020-H-35-49-white.Rdata",
+                                         "current-results-2016-2020-H-50-64-white.Rdata",
+                                         "current-results-2016-2020-H-65+-white.Rdata"
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('-H-',file),Housing:='Apartment']

> results[grepl('-S-',file),Housing:='Single-family']

> results[grepl('18-34',file),Age:='18-34']

> results[grepl('35-49',file),Age:='35-49']

> results[grepl('50-64',file),Age:='50-64']

> results[grepl('65+',file),Age:='65+']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results[Years=='2016-2020'], aes(y = Estimate, x = Age, color = Subset, shape = Housing))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   # scale_shape_manual(values = 1:2)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('2016 Age')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Subset)

> ggsave(plot = g1,filename = 'figures/FigS3b.png',dpi=300, width = 11, height = 5, units = 'in')

> g2 = ggplot(results[Years=='2012-2016'], aes(y = Estimate, x = Age, color = Subset, shape = Housing))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   #  scale_shape_manual(values = 1:2)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('2012 Age')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Subset)

> ggsave(plot = g2,filename = 'figures/FigS3a.png',dpi=300, width = 11, height = 5, units = 'in')

> toc()
2318.021 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3328490   177.8    11001588   587.6    58645897  3132.1
Vcells 5009187914 38217.1 12380529682 94456.0 12380342215 94454.6

> # Figure S4
> tic() # time scripts for each figure

> source('EST-future-results-2012-2016-dwelling-age.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3330394   177.9    11001588   587.6    58645897  3132.1
Vcells 5009188702 38217.1 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'vb.tsmart_dwelling_type'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> data[,Age_strata := ifelse(Age_year1 <= 34, '18-34', 
+                            ifelse(Age_year1 %in% 35:49, '35-49',
+                                   ifelse(Age_year1 %in% 50:64, '50-64', '65+')))]

>   # split data
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

>  # rm(data)
>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3339257   178.4    11001588   587.6    58645897  3132.1
Vcells 6532875283 49841.9 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems1=dems

> reps1=reps

> oths1=oths

> for(i in c('S','H')){
+   for(j in unique(data$Age_strata)[!is.na(unique(data$Age_strata))]){
+     dems=dems1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     reps=reps1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     oths=oths1[vb.tsmart_dwelling_type==i & Age_strata==j]
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/future-results-2012-2016-',i,'-',j,'.Rdata'))
+   }
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3328634   177.8    22138507  1182.4    58645897  3132.1
Vcells 5115717286 39029.9 12380529682 94456.0 12380342215 94454.6

> source('FIG-future-results-age-dwelling.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3329634   177.9    17710806   945.9    58645897  3132.1
Vcells 5115698005 39029.7 12380529682 94456.0 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c("future-results-2012-2016-S-18-34.Rdata",
+                                         "future-results-2012-2016-S-35-49.Rdata",
+                                         "future-results-2012-2016-S-50-64.Rdata",
+                                         "future-results-2012-2016-S-65+.Rdata",
+                                         
+                                         "future-results-2012-2016-H-18-34.Rdata",
+                                         "future-results-2012-2016-H-35-49.Rdata",
+                                         "future-results-2012-2016-H-50-64.Rdata",
+                                         "future-results-2012-2016-H-65+.Rdata"
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('-H-',file),Housing:='Apartment']

> results[grepl('-S-',file),Housing:='Single-family']

> results[grepl('18-34',file),Age:='18-34']

> results[grepl('35-49',file),Age:='35-49']

> results[grepl('50-64',file),Age:='50-64']

> results[grepl('65+',file),Age:='65+']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g2 = ggplot(results[Years=='2012-2016'], aes(y = Estimate, x = Age, color = Subset, shape = Housing))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   #  scale_shape_manual(values = 1:2)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('2012 Age')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Subset, scales = 'free_x')

> ggsave(plot = g2,filename = 'figures/FigS4.png',dpi=300, width = 11, height = 5, units = 'in')

> toc()
1056.89 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3329501   177.9    14168645   756.7    58645897  3132.1
Vcells 5115703868 39029.8 12380529682 94456.0 12380342215 94454.6

> # Figure S5
> tic() # time scripts for each figure

> source('EST-future-results-2012-2016-dwelling-age-white.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3331454   178.0    11334916   605.4    58645897  3132.1
Vcells 5115717287 39029.9 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'vb.tsmart_dwelling_type'))
|--------------------------------------------------|
|==================================================|

> data = data[Race=='White']

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> data[,Age_strata := ifelse(Age_year1 <= 34, '18-34', 
+                            ifelse(Age_year1 %in% 35:49, '35-49',
+                                   ifelse(Age_year1 %in% 50:64, '50-64', '65+')))]

>   # split data
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

>  # rm(data)
>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3340281   178.4    11334916   605.4    58645897  3132.1
Vcells 6216935529 47431.5 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # republican exposure
>   dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems1=dems

> reps1=reps

> oths1=oths

> for(i in c('S','H')){
+   for(j in unique(data$Age_strata)[!is.na(unique(data$Age_strata))]){
+     dems=dems1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     reps=reps1[vb.tsmart_dwelling_type==i & Age_strata==j]
+     oths=oths1[vb.tsmart_dwelling_type==i & Age_strata==j]
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/future-results-2012-2016-',i,'-',j,'-white.Rdata'))
+   }
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3330010   177.9    15785867   843.1    58645897  3132.1
Vcells 5222378229 39843.6 12380529682 94456.0 12380342215 94454.6

> source('FIG-future-results-age-dwelling-whites.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3330686   177.9    12628694   674.5    58645897  3132.1
Vcells 5222226448 39842.5 12380529682 94456.0 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c("future-results-2012-2016-S-18-34-white.Rdata",
+                                         "future-results-2012-2016-S-35-49-white.Rdata",
+                                         "future-results-2012-2016-S-50-64-white.Rdata",
+                                         "future-results-2012-2016-S-65+-white.Rdata",
+                                         
+                                         "future-results-2012-2016-H-18-34-white.Rdata",
+                                         "future-results-2012-2016-H-35-49-white.Rdata",
+                                         "future-results-2012-2016-H-50-64-white.Rdata",
+                                         "future-results-2012-2016-H-65+-white.Rdata"
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('-H-',file),Housing:='Apartment']

> results[grepl('-S-',file),Housing:='Single-family']

> results[grepl('18-34',file),Age:='18-34']

> results[grepl('35-49',file),Age:='35-49']

> results[grepl('50-64',file),Age:='50-64']

> results[grepl('65+',file),Age:='65+']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g2 = ggplot(results[Years=='2012-2016'], aes(y = Estimate, x = Age, color = Subset, shape = Housing))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   #  scale_shape_manual(values = 1:2)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('2012 Age')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Subset, scales = 'free_x')

> ggsave(plot = g2,filename = 'figures/FigS5.png',dpi=300, width = 11, height = 5, units = 'in')

> toc()
782.882 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3330548   177.9    10102956   539.6    58645897  3132.1
Vcells 5222230213 39842.5 12380529682 94456.0 12380342215 94454.6

> # Figure S6 previously created (otherwise run commented out scripts)
> #tic() # time scripts for each figure
> #source('EST-current-results-2012-201 .... [TRUNCATED] 

> source('EST-future-results-2012-2016-race-subset.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3332548   178.0    10102956   539.6    58645897  3132.1
Vcells 5222246118 39842.6 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020',"DemSpExpWhite_nohh_year1" ,   "DemSpExpWhiteDiff_nohh"  ,    "DemSpExpBlack_nohh_year1" ,   "DemSpExpBlackDiff_nohh"     ,
+                                                                            "DemSpExpHispanic_nohh_year1", "DemSpExpHispanicDiff_nohh"  , "DemSpExpAsian_nohh_year1"  ,  "DemSpExpAsianDiff_nohh"  ,    "RepSpExpWhite_nohh_year1" ,  
+                                                                            "RepSpExpWhiteDiff_nohh"  ,    "RepSpExpBlack_nohh_year1"   , "RepSpExpBlackDiff_nohh"   ,   "RepSpExpHispanic_nohh_year1", "RepSpExpHispanicDiff_nohh",  
+                                                                            "RepSpExpAsian_nohh_year1" ,   "RepSpExpAsianDiff_nohh",'Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data = data[Race%in%c('White','Black','Hispanic','Asian')]

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> 
> 
> 
> for(r in c('Asian', 'Black','Hispanic','White')){
+   data[['DemTreatment']] = data[[paste0('DemSpExp', r,'_nohh_year1')]]
+   data[['RepTreatment']] = data[[paste0('RepSpExp', r,'_nohh_year1')]]
+   data[['DemSpExpDiff_nohh']] = data[[paste0('DemSpExp', r,'Diff_nohh')]]
+   data[['RepSpExpDiff_nohh']] = data[[paste0('RepSpExp', r,'Diff_nohh')]]
+   
+   
+   data[, Interaction:=ifelse(Race==r, r, paste0('Non-',r))]
+   
+   
+   # split data
+   dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']
+   reps = data[Party_year1=='Republican' & Party_2016=='Republican']
+   oths = data[Party_year1=='Other' & Party_2016=='Other']
+   
+   
+   #rm(data)
+   gc()
+   
+   # construct grouping variables 
+   # we can drop rows where any of these are missing
+   
+   
+   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]
+   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+   
+   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+ 
+   # spatial seg treatment
+   # democrat exposure
+   dems[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,DemSpDecile:=cut(DemTreatment,breaks=unique(as.numeric(quantile(DemTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+   # republican exposure
+   dems[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   reps[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   oths[,RepSpDecile:=cut(RepTreatment,breaks=unique(as.numeric(quantile(RepTreatment, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+   
+   
+   
+   #####
+   # make groups
+   # spatial seg treatment
+   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+   
+ dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ 
+   
+   
+ #   ###
+   # estimate models
+   
+   # spatial seg treatment
+   ModelDemSpExpDems = summary(felm(DemDiff2 ~ Interaction:(DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpReps = summary(felm(DemDiff2 ~ Interaction:(DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   ModelDemSpExpOths = summary(felm(DemDiff2 ~ Interaction:(DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+   
+   ModelRepSpExpDems = summary(felm(RepDiff2 ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpReps = summary(felm(RepDiff2 ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   ModelRepSpExpOths = summary(felm(RepDiff2 ~ Interaction:(RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff)|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/future-results-2012-2016-',r,'-subset.Rdata'))
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3330813   177.9    29373880  1568.8    58645897  3132.1
Vcells 5373833572 40999.1 12380529682 94456.0 12380342215 94454.6

> source('FIG-future-results-race-subset.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used  (Mb)  gc trigger  (Mb)    max used    (Mb)
Ncells    3331811   178    23499104  1255    58645897  3132.1
Vcells 5373820810 40999 12380529682 94456 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c(
+                                         'future-results-2012-2016-Asian-subset.Rdata',
+                             
+                                         'future-results-2012-2016-Black-subset.Rdata',
+                                      
+                                         'future-results-2012-2016-Hispanic-subset.Rdata',
+                                     
+                                         'future-results-2012-2016-White-subset.Rdata'
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('InteractionWhite:DemSpExpDiff_nohh','InteractionWhite:RepSpExpDiff_nohh',
+                                    'InteractionNon-White:DemSpExpDiff_nohh','InteractionNon-White:RepSpExpDiff_nohh',
+                                    
+                                    'InteractionBlack:DemSpExpDiff_nohh','InteractionBlack:RepSpExpDiff_nohh',
+                                    'InteractionNon-Black:DemSpExpDiff_nohh','InteractionNon-Black:RepSpExpDiff_nohh',
+                                    
+                                    
+                                    'InteractionHispanic:DemSpExpDiff_nohh','InteractionHispanic:RepSpExpDiff_nohh',
+                                    'InteractionNon-Hispanic:DemSpExpDiff_nohh','InteractionNon-Hispanic:RepSpExpDiff_nohh',
+                                    
+                                    
+                                    'InteractionAsian:DemSpExpDiff_nohh','InteractionAsian:RepSpExpDiff_nohh',
+                                    'InteractionNon-Asian:DemSpExpDiff_nohh','InteractionNon-Asian:RepSpExpDiff_nohh'
+                                    )]

> results[!grepl('Non-',Covariate),Type:='Ingroup']

> results[grepl('Non-',Covariate),Type:='Outgroup']

> results[grepl('White',file),Race:='White']

> results[grepl('Asian',file),Race:='Asian']

> results[grepl('Black',file),Race:='Black']

> results[grepl('Hispanic',file),Race:='Hispanic']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g = ggplot(results %>%
+              mutate(Race = paste0(Race,' partisan exposure')), aes(y = Estimate, x = Subset, color = Subset, shape = Type))+
+   geom_point( size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Effect of partisan exposure')+
+   xlab('Original party')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 16, family = 'serif'),panel.spacing = unit(.5, "lines"), strip.text.y = element_text(size = 10, angle = 0), legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Race, scales = 'free_x')

> ggsave(plot = g,filename = 'figures/FigS7.png',dpi=300, width = 12, height = 5, units = 'in')

> toc()
3096.906 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3331693   178.0    15039428   803.2    58645897  3132.1
Vcells 5373835045 40999.2 12380529682 94456.0 12380342215 94454.6

> # Figure S8 
> tic() # time scripts for each figure

> source('EST-future-results-2012-2016-by-district.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333518   178.1    12031543   642.6    58645897  3132.1
Vcells 5373839494 40999.2 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff', 'StateDistrict'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> #rm(data)
> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3342492   178.6     9625235   514.1    58645897  3132.1
Vcells 6853253414 52286.2 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables ba
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> dems1=dems

> reps1=reps

> oths1=oths

> for(s in unique(data$StateDistrict)[!is.na(unique(data$StateDistrict))]){
+   
+   dems = dems1[ StateDistrict ==s]
+   reps = reps1[ StateDistrict==s]
+   oths = oths1[  StateDistrict==s]
+   
+   
+   ModelDemSpExpDems = try(summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                          HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                          DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+   ModelDemSpExpReps = try(summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                          HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                          DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+   ModelDemSpExpOths = try(summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                          HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                          DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T))
+   
+   ModelRepSpExpDems = try(summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                          HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                          DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+   ModelRepSpExpReps = try(summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                          HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                          DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+   ModelRepSpExpOths = try(summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                          HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                          DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T))
+   
+   
+   
+   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/future-results-2012-2016-district-',s,'.Rdata'))
+   
+   rm(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths)
+ }
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in chol.default(mat[ok, ok]) : 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(RepDiff2 ~ RepSpExpDiff_nohh + hh.r.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
3: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning messages:
1: In summary.felm(felm(RepDiff2 ~ RepSpExpDiff_nohh + hh.r.diff +  :
  can't compute cluster F-test
2: In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'object' in selecting a method for function 'summary': 'a' must have dims > 0
In addition: Warning message:
In chol.default(mat, pivot = TRUE, tol = tol) :
  the matrix is either rank-deficient or indefinite

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger  (Mb)    max used    (Mb)
Ncells    3333150   178.1    23310394  1245    58645897  3132.1
Vcells 5481072757 41817.3 12380529682 94456 12380342215 94454.6

> source('FIG-future-results-districts.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger  (Mb)    max used    (Mb)
Ncells    3332846   178.0    18648316   996    58645897  3132.1
Vcells 5480352772 41811.8 12380529682 94456 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> library(scales)

> library(tidyr)

> files = paste0('results/',list.files('results'))

> files = files[grepl('district', files)]

> files = files[grepl('future',files)]

> files = files[(grepl('2008',files)&grepl('2012',files))|(grepl('2012',files) & grepl('2016',files))|(grepl('2016',files)&grepl('2020',files))]

> files = files[!grepl('NA.Rdata',files)]

> files = files[!grepl('aspatial',files)]

> files=files[!grepl('2008',files)]

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   print(file)
+   l = rbindlist(lapply(models, FUN=function(x){
+     #print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,district:=str_replace(str_replace(file,paste0('results/future-results-',Year1, '-',Year2,'-district-'),''),'.Rdata','')]
+ l[,file:=file]
+ 
+ return(l)
+ }))
[1] "results/future-results-2012-2016-district-CA1.Rdata"
[1] "results/future-results-2012-2016-district-CA10.Rdata"
[1] "results/future-results-2012-2016-district-CA11.Rdata"
[1] "results/future-results-2012-2016-district-CA12.Rdata"
[1] "results/future-results-2012-2016-district-CA13.Rdata"
[1] "results/future-results-2012-2016-district-CA14.Rdata"
[1] "results/future-results-2012-2016-district-CA15.Rdata"
[1] "results/future-results-2012-2016-district-CA16.Rdata"
[1] "results/future-results-2012-2016-district-CA17.Rdata"
[1] "results/future-results-2012-2016-district-CA18.Rdata"
[1] "results/future-results-2012-2016-district-CA19.Rdata"
[1] "results/future-results-2012-2016-district-CA2.Rdata"
[1] "results/future-results-2012-2016-district-CA20.Rdata"
[1] "results/future-results-2012-2016-district-CA21.Rdata"
[1] "results/future-results-2012-2016-district-CA22.Rdata"
[1] "results/future-results-2012-2016-district-CA23.Rdata"
[1] "results/future-results-2012-2016-district-CA24.Rdata"
[1] "results/future-results-2012-2016-district-CA25.Rdata"
[1] "results/future-results-2012-2016-district-CA26.Rdata"
[1] "results/future-results-2012-2016-district-CA27.Rdata"
[1] "results/future-results-2012-2016-district-CA28.Rdata"
[1] "results/future-results-2012-2016-district-CA29.Rdata"
[1] "results/future-results-2012-2016-district-CA3.Rdata"
[1] "results/future-results-2012-2016-district-CA30.Rdata"
[1] "results/future-results-2012-2016-district-CA31.Rdata"
[1] "results/future-results-2012-2016-district-CA32.Rdata"
[1] "results/future-results-2012-2016-district-CA33.Rdata"
[1] "results/future-results-2012-2016-district-CA34.Rdata"
[1] "results/future-results-2012-2016-district-CA35.Rdata"
[1] "results/future-results-2012-2016-district-CA36.Rdata"
[1] "results/future-results-2012-2016-district-CA37.Rdata"
[1] "results/future-results-2012-2016-district-CA38.Rdata"
[1] "results/future-results-2012-2016-district-CA39.Rdata"
[1] "results/future-results-2012-2016-district-CA4.Rdata"
[1] "results/future-results-2012-2016-district-CA40.Rdata"
[1] "results/future-results-2012-2016-district-CA41.Rdata"
[1] "results/future-results-2012-2016-district-CA42.Rdata"
[1] "results/future-results-2012-2016-district-CA43.Rdata"
[1] "results/future-results-2012-2016-district-CA44.Rdata"
[1] "results/future-results-2012-2016-district-CA45.Rdata"
[1] "results/future-results-2012-2016-district-CA46.Rdata"
[1] "results/future-results-2012-2016-district-CA47.Rdata"
[1] "results/future-results-2012-2016-district-CA48.Rdata"
[1] "results/future-results-2012-2016-district-CA49.Rdata"
[1] "results/future-results-2012-2016-district-CA5.Rdata"
[1] "results/future-results-2012-2016-district-CA50.Rdata"
[1] "results/future-results-2012-2016-district-CA51.Rdata"
[1] "results/future-results-2012-2016-district-CA52.Rdata"
[1] "results/future-results-2012-2016-district-CA53.Rdata"
[1] "results/future-results-2012-2016-district-CA6.Rdata"
[1] "results/future-results-2012-2016-district-CA7.Rdata"
[1] "results/future-results-2012-2016-district-CA8.Rdata"
[1] "results/future-results-2012-2016-district-CA9.Rdata"
[1] "results/future-results-2012-2016-district-FL1.Rdata"
[1] "results/future-results-2012-2016-district-FL10.Rdata"
[1] "results/future-results-2012-2016-district-FL11.Rdata"
[1] "results/future-results-2012-2016-district-FL12.Rdata"
[1] "results/future-results-2012-2016-district-FL13.Rdata"
[1] "results/future-results-2012-2016-district-FL14.Rdata"
[1] "results/future-results-2012-2016-district-FL15.Rdata"
[1] "results/future-results-2012-2016-district-FL16.Rdata"
[1] "results/future-results-2012-2016-district-FL17.Rdata"
[1] "results/future-results-2012-2016-district-FL18.Rdata"
[1] "results/future-results-2012-2016-district-FL19.Rdata"
[1] "results/future-results-2012-2016-district-FL2.Rdata"
[1] "results/future-results-2012-2016-district-FL20.Rdata"
[1] "results/future-results-2012-2016-district-FL21.Rdata"
[1] "results/future-results-2012-2016-district-FL22.Rdata"
[1] "results/future-results-2012-2016-district-FL23.Rdata"
[1] "results/future-results-2012-2016-district-FL24.Rdata"
[1] "results/future-results-2012-2016-district-FL25.Rdata"
[1] "results/future-results-2012-2016-district-FL26.Rdata"
[1] "results/future-results-2012-2016-district-FL27.Rdata"
[1] "results/future-results-2012-2016-district-FL3.Rdata"
[1] "results/future-results-2012-2016-district-FL4.Rdata"
[1] "results/future-results-2012-2016-district-FL5.Rdata"
[1] "results/future-results-2012-2016-district-FL6.Rdata"
[1] "results/future-results-2012-2016-district-FL7.Rdata"
[1] "results/future-results-2012-2016-district-FL8.Rdata"
[1] "results/future-results-2012-2016-district-FL9.Rdata"
[1] "results/future-results-2012-2016-district-KS1.Rdata"
[1] "results/future-results-2012-2016-district-KS2.Rdata"
[1] "results/future-results-2012-2016-district-KS3.Rdata"
[1] "results/future-results-2012-2016-district-KS4.Rdata"
[1] "results/future-results-2012-2016-district-NC1.Rdata"
[1] "results/future-results-2012-2016-district-NC10.Rdata"
[1] "results/future-results-2012-2016-district-NC11.Rdata"
[1] "results/future-results-2012-2016-district-NC12.Rdata"
[1] "results/future-results-2012-2016-district-NC13.Rdata"
[1] "results/future-results-2012-2016-district-NC2.Rdata"
[1] "results/future-results-2012-2016-district-NC3.Rdata"
[1] "results/future-results-2012-2016-district-NC4.Rdata"
[1] "results/future-results-2012-2016-district-NC5.Rdata"
[1] "results/future-results-2012-2016-district-NC6.Rdata"
[1] "results/future-results-2012-2016-district-NC7.Rdata"
[1] "results/future-results-2012-2016-district-NC8.Rdata"
[1] "results/future-results-2012-2016-district-NC9.Rdata"
[1] "results/future-results-2012-2016-district-NY1.Rdata"
[1] "results/future-results-2012-2016-district-NY10.Rdata"
[1] "results/future-results-2012-2016-district-NY11.Rdata"
[1] "results/future-results-2012-2016-district-NY12.Rdata"
[1] "results/future-results-2012-2016-district-NY13.Rdata"
[1] "results/future-results-2012-2016-district-NY14.Rdata"
[1] "results/future-results-2012-2016-district-NY15.Rdata"
[1] "results/future-results-2012-2016-district-NY16.Rdata"
[1] "results/future-results-2012-2016-district-NY17.Rdata"
[1] "results/future-results-2012-2016-district-NY18.Rdata"
[1] "results/future-results-2012-2016-district-NY19.Rdata"
[1] "results/future-results-2012-2016-district-NY2.Rdata"
[1] "results/future-results-2012-2016-district-NY20.Rdata"
[1] "results/future-results-2012-2016-district-NY21.Rdata"
[1] "results/future-results-2012-2016-district-NY22.Rdata"
[1] "results/future-results-2012-2016-district-NY23.Rdata"
[1] "results/future-results-2012-2016-district-NY24.Rdata"
[1] "results/future-results-2012-2016-district-NY25.Rdata"
[1] "results/future-results-2012-2016-district-NY26.Rdata"
[1] "results/future-results-2012-2016-district-NY27.Rdata"
[1] "results/future-results-2012-2016-district-NY3.Rdata"
[1] "results/future-results-2012-2016-district-NY4.Rdata"
[1] "results/future-results-2012-2016-district-NY5.Rdata"
[1] "results/future-results-2012-2016-district-NY6.Rdata"
[1] "results/future-results-2012-2016-district-NY7.Rdata"
[1] "results/future-results-2012-2016-district-NY8.Rdata"
[1] "results/future-results-2012-2016-district-NY9.Rdata"

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Exposure\nto Democrats']

> results[grepl('RepSpExp',Model),Exposure.Type:='Exposure\nto Republicans']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> d = fread('1976-2018-house.csv')[year%in%2012:2020 & state %in% c('California','North Carolina','New York','Florida','Kansas')]

> d[,pct:=candidatevotes/totalvotes]

> setorderv(d, c('year','state','district','pct'),c(1,-1,-1,-1))

> d = d[,list(candidate=.N,
+             margin = pct[1]-pct[2],
+             winner = party[1],
+             winning.pct=pct[1],
+             runnerup = party[2],
+             runerup.pct = pct[2]),by=c('year','state','district')]

> d1 = d[year %in% 2012:2016][,list(
+   avg.margin=mean(margin,na.rm=T),
+   min.margin=min(margin,na.rm=T),
+   dem.control = mean(winner=='democrat',na.rm=T),
+   rep.control = mean(winner=='republican',na.rm=T),
+   n=.N,
+   nas=sum(is.na(margin))
+     
+     
+     
+   ),by=c('state','district')][,Years:='2012-2016']

> d = d1

> d[,district:=case_when(state=='California'~paste0('CA',district),
+                                 state=='New York'~paste0('NY',district),
+                                 state=='Kansas'~paste0('KS',district),
+                                 state=='North Carolina'~paste0('NC',district),
+                                 state=='Florida'~paste0('FL',district)
+ )]

> results = merge(results,d,all.x=T,by=c('Years','district'))

> results[,Type := case_when(min.margin>.2 & (dem.control==1|rep.control==1)~'Uncompetitive',
+                           T ~ 'Competitive')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> library(grid)

> g = ggplot(results, aes(x = Estimate, weight = N, fill = Subset, color = Subset, linetype = Type))+
+   geom_density(bw=.025,alpha=.2)+
+   theme_bw()+
+   geom_vline(xintercept = 0, linetype = 'dashed')+
+   facet_grid(Exposure.Type ~ Years + Subset)+
+   scale_color_manual(values=colors)+
+   scale_fill_manual(values=colors)+
+   xlab('District-level effect of partisan exposure')+
+   ylab('Density')+
+   theme(panel.spacing = unit(1, 'lines'))+
+   guides(color = 'none', fill = 'none')+
+   
+   theme(legend.position = 'bottom',legend.title = element_blank(),
+         text = element_text(size=18, family='serif'), 
+         
+         strip.text.y = element_text(size=12,angle = 0))

> ggsave(plot = g, filename = 'figures/FigS8.png',dpi=300,width = 11, height  =5 ,units='in')

> toc()
1509.886 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333019   178.1    11934923   637.4    58645897  3132.1
Vcells 5480444206 41812.5 12380529682 94456.0 12380342215 94454.6

> # Figure S9
> tic() # time scripts for each figure 

> # If figure 4 is not yet created, run commented out scripts below
> #source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)
 .... [TRUNCATED] 

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger  (Mb)    max used    (Mb)
Ncells    3334537   178.1     9547939   510    58645897  3132.1
Vcells 5480455993 41812.6 12380529682 94456 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> for(y2 in c(2014,2016,2018,2020)){
+ 
+ data = fread(paste0('panel-analysis-2008-',y2,'.csv.gz'),select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff'))
+ 
+ 
+ data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']
+ data[countyfips=='',countyfips:=NA]
+ data[ZipCode=='',ZipCode:=NA]
+ data[Gender=='',Gender:=NA]
+ data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]
+ data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]
+ data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]
+ 
+ 
+ # split data
+ dems = data[Party_year1=='Democrat']
+ reps = data[Party_year1=='Republican']
+ oths = data[Party_year1=='Other']
+ 
+ rm(data)
+ gc()
+ 
+ # construct grouping variables 
+ # we can drop rows where any of these are missing
+ 
+ dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]
+ reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]
+ oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]
+ 
+ dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ 
+ # spatial seg treatment
+ # democrat exposure
+ dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ # republican exposure
+ dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ 
+ 
+ #####
+ # make groups
+ # spatial seg treatment
+ dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]
+ reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]
+ oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]
+ 
+ dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]
+ reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]
+ oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]
+ 
+ 
+ 
+ ## output SDs
+ 
+ 
+ #   ###
+ # estimate models
+ 
+ 
+ 
+ # spatial seg treatment
+ ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ 
+ ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2008-',y2,'.Rdata'))
+ }
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3266151   174.5    23125748  1235.1    58645897  3132.1
Vcells 5652977421 43128.8 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-by-year.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3267485   174.6    18500599   988.1    58645897  3132.1
Vcells 5652902758 43128.3 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> for(y2 in c(2014,2018,2020)){
+ 
+ data = fread(paste0('panel-analysis-2012-',y2,'.csv.gz'),select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
+ 
+ 
+ data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']
+ data[countyfips=='',countyfips:=NA]
+ data[ZipCode=='',ZipCode:=NA]
+ data[Gender=='',Gender:=NA]
+ 
+ 
+ data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]
+ data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]
+ data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]
+ 
+ 
+ # split data
+ dems = data[Party_year1=='Democrat']
+ reps = data[Party_year1=='Republican']
+ oths = data[Party_year1=='Other']
+ 
+ rm(data)
+ gc()
+ 
+ # construct grouping variables
+ # we can drop rows where any of these are missing
+ 
+ 
+ dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]
+ reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+ oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+ 
+ dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ 
+ # spatial seg treatment
+ # democrat exposure
+ dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ # republican exposure
+ dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ 
+ 
+ #####
+ # make groups
+ # spatial seg treatment
+ dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+ 
+ dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ 
+ 
+ 
+ ## output SDs
+ 
+ 
+ #   ###
+ # estimate models
+ 
+ 
+ 
+ # spatial seg treatment
+ ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ 
+ ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-',y2,'.Rdata'))
+ }
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333435   178.1    35672952  1905.2    58645897  3132.1
Vcells 5894883433 44974.4 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2016-by-year.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3334970   178.2    28538362  1524.2    58645897  3132.1
Vcells 5894881256 44974.4 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> for(y2 in c(2018)){
+ 
+ data = fread(paste0('panel-analysis-2016-',y2,'.csv.gz'),select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
+ 
+ 
+ data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']
+ data[countyfips=='',countyfips:=NA]
+ data[ZipCode=='',ZipCode:=NA]
+ data[Gender=='',Gender:=NA]
+ 
+ 
+ data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]
+ data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]
+ data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]
+ 
+ 
+ # split data
+ dems = data[Party_year1=='Democrat']
+ reps = data[Party_year1=='Republican']
+ oths = data[Party_year1=='Other']
+ 
+ rm(data)
+ gc()
+ 
+ # construct grouping variables b
+ # we can drop rows where any of these are missing
+ 
+ 
+ dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]
+ reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+ oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]
+ 
+ dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ 
+ # spatial seg treatment
+ # democrat exposure
+ dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ # republican exposure
+ dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
+ 
+ 
+ 
+ #####
+ # make groups
+ # spatial seg treatment
+ dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]
+ 
+ dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]
+ 
+ 
+ 
+ ## output SDs
+ 
+ 
+ #   ###
+ # estimate models
+ 
+ 
+ 
+ # spatial seg treatment
+ ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ 
+ ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-',y2,'.Rdata'))
+ }
|--------------------------------------------------|
|==================================================|

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333420   178.1    26371355  1408.4    58645897  3132.1
Vcells 5979134510 45617.2 12380529682 94456.0 12380342215 94454.6

> source('FIG-current-results-all-years.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3334588   178.1    21097084  1126.8    58645897  3132.1
Vcells 5979134447 45617.2 12380529682 94456.0 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c("current-results-2008-2012.Rdata",
+                                         "current-results-2008-2014.Rdata",
+                                         "current-results-2008-2016.Rdata",
+                                         "current-results-2008-2018.Rdata",
+                                         "current-results-2008-2020.Rdata",
+   
+   
+   
+                                         "current-results-2012-2014.Rdata",
+                                         "current-results-2012-2016.Rdata",
+                                         "current-results-2012-2018.Rdata",
+                                         "current-results-2012-2020.Rdata",
+                                         
+                                         "current-results-2016-2018.Rdata",
+                                         "current-results-2016-2020.Rdata"
+                                        
+   
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+ l[,Years:=substr(file,nchar(file)-14,nchar(file)-6)]  
+ l[,file:=file]
+ l[,Year1:=substr(Years,1,4)]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results[Year1=='2008'], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/FigS9a.png',dpi=300, width = 11, height = 6, units = 'in')

> g2 = ggplot(results[Year1=='2012'], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g2,filename = 'figures/FigS9b.png',dpi=300, width = 11, height = 6, units = 'in')

> g3 = ggplot(results[Year1=='2016'], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g3,filename = 'figures/FigS9c.png',dpi=300, width = 11, height = 6, units = 'in')

> toc()
6945.896 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333898   178.1    16053520   857.4    58645897  3132.1
Vcells 5979147273 45617.3 12380529682 94456.0 12380342215 94454.6

> # Figure S10
> tic() # time scripts for each figure 

> source('EST-current-results-2008-2012-by-state.R', echo = T,  max.deparse.length=10000)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335367   178.2    12842816   685.9    58645897  3132.1
Vcells 5979130159 45617.2 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2008-2012.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3342516   178.6    10274253   548.8    58645897  3132.1
Vcells 6457815595 49269.3 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> ## output SDs
> 
> 
> #   ###
> # estimate models
> 
> 
> dems1=dems

> oths1=oths

> reps1=reps

> for(s in c('CA','FL','KS','NC','NY')){
+   dems = dems1[State==s]
+   reps = reps1[State==s]
+   oths = oths1[State==s]
+ 
+ # spatial seg treatment
+ ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ 
+ ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2008-2012-',s,'.Rdata'))
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333899   178.1    17215527   919.5    58645897  3132.1
Vcells 6066404449 46283.0 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-by-state.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335477   178.2    13772422   735.6    58645897  3132.1
Vcells 6066398553 46283.0 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3344190   178.6    11017938   588.5    58645897  3132.1
Vcells 6740250808 51424.1 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> 
> #   ###
> # estimate models
> 
> 
> dems1=dems

> oths1=oths

> reps1=reps

> for(s in c('CA','FL','KS','NC','NY')){
+   dems = dems1[State==s]
+   reps = reps1[State==s]
+   oths = oths1[State==s]
+ 
+ # spatial seg treatment
+ ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ 
+ ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-',s,'.Rdata'))
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3334129   178.1    22171055  1184.1    58645897  3132.1
Vcells 6164819505 47033.9 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2016-2020-by-state.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335477   178.2    17736844   947.3    58645897  3132.1
Vcells 6164723272 47033.2 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3343631   178.6    14189476   757.8    58645897  3132.1
Vcells 7022438531 53577.0 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> 
> #   ###
> # estimate models
> 
> 
> dems1=dems

> oths1=oths

> reps1=reps

> for(s in c('CA','FL','KS','NC','NY')){
+   dems = dems1[State==s]
+   reps = reps1[State==s]
+   oths = oths1[State==s]
+ 
+ # spatial seg treatment
+ ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
+ 
+ ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
+ 
+ 
+ 
+ save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-',s,'.Rdata'))
+ }

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3333923   178.1    23710413  1266.3    58645897  3132.1
Vcells 6274554491 47871.1 12380529682 94456.0 12380342215 94454.6

> source('FIG-current-results-by-state.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335571   178.2    18968331  1013.1    58645897  3132.1
Vcells 6274543088 47871.0 12380529682 94456.0 12380342215 94454.6

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c("current-results-2008-2012-CA.Rdata",
+                                         "current-results-2008-2012-KS.Rdata",
+                                         "current-results-2008-2012-NC.Rdata",
+                                         "current-results-2008-2012-NY.Rdata",
+                                         "current-results-2008-2012-FL.Rdata",
+   
+   
+   
+                                         "current-results-2012-2016-CA.Rdata",
+                                         "current-results-2012-2016-KS.Rdata",
+                                         "current-results-2012-2016-NC.Rdata",
+                                         "current-results-2012-2016-NY.Rdata",
+                                         "current-results-2012-2016-FL.Rdata",
+                                         
+                                         "current-results-2016-2020-CA.Rdata",
+                                         "current-results-2016-2020-KS.Rdata",
+                                         "current-results-2016-2020-NC.Rdata",
+                                         "current-results-2016-2020-NY.Rdata",
+                                         "current-results-2016-2020-FL.Rdata"
+   
+                                         ))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+ l[grepl('2020',file),Year1:='2016']
+ l[grepl('2020',file),Year2:='2020'] 
+ l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+ l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+ l[,file:=file]
+ 
+ return(l)
+ }))

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('-CA',file),State:='CA']

> results[grepl('-NY',file),State:='NY']

> results[grepl('-FL',file),State:='FL']

> results[grepl('-NC',file),State:='NC']

> results[grepl('-KS',file),State:='KS']

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> results[,Years:=paste0(Year1,'-',Year2)]

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results[State=='CA' ], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/FigS10a.png',dpi=300, width = 11, height = 6, units = 'in')

> g2 = ggplot(results[State=='FL' ], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g2,filename = 'figures/FigS10b.png',dpi=300, width = 11, height = 6, units = 'in')

> g3 = ggplot(results[State=='NC' ], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g3,filename = 'figures/FigS10d.png',dpi=300, width = 11, height = 6, units = 'in')

> g4 = ggplot(results[State=='KS' ], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g4,filename = 'figures/FigS10c.png',dpi=300, width = 11, height = 6, units = 'in')

> g5 = ggplot(results[State=='NY' ], aes(y = Estimate, x = Subset, color = Subset))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g5,filename = 'figures/FigS10e.png',dpi=300, width = 11, height = 6, units = 'in')

> toc()
5490.206 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335242   178.2     9711786   518.7    58645897  3132.1
Vcells 6274580390 47871.3 12380529682 94456.0 12380342215 94454.6

> # Table S12
> tic() # time scripts for each figure 

> # If figure 4 is not yet created, run commented out scripts below
> #source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)
 .... [TRUNCATED] 

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336723   178.3     9711786   518.7    58645897  3132.1
Vcells 6274559340 47871.1 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemExpDiff', 'RepExpDiff','DemExp_year1','RepExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345440   178.7     9711786   518.7    58645897  3132.1
Vcells 6948411599 53012.2 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemExpDems = summary(felm(DemDiff ~ DemExpDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemExpReps = summary(felm(DemDiff ~ DemExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemExpOths = summary(felm(DemDiff ~ DemExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepExpDems = summary(felm(RepDiff ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepExpReps = summary(felm(RepDiff ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepExpOths = summary(felm(RepDiff ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemExpDems, ModelDemExpReps, ModelDemExpOths, 
+      ModelRepExpDems, ModelRepExpReps, ModelRepExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-aspatial.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335263   178.2    16282463   869.6    58645897  3132.1
Vcells 6372884409 48621.3 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-block-group.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336740   178.3    13025971   695.7    58645897  3132.1
Vcells 6372884175 48621.3 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemBlockGroupDiff','DemBlockGroup_year1','RepBlockGroupDiff','RepBlockGroup_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345457   178.7    10420777   556.6    58645897  3132.1
Vcells 7046736435 53762.4 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemBlockGroupDems = summary(felm(DemDiff ~ DemBlockGroupDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockGroupReps = summary(felm(DemDiff ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockGroupOths = summary(felm(DemDiff ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockGroupDems = summary(felm(RepDiff ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockGroupReps = summary(felm(RepDiff ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockGroupOths = summary(felm(RepDiff ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemBlockGroupDems, ModelDemBlockGroupReps, ModelDemBlockGroupOths, 
+        ModelRepBlockGroupDems, ModelRepBlockGroupReps, ModelRepBlockGroupOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-block-group.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335258   178.2    20982312  1120.6    58645897  3132.1
Vcells 6471206991 49371.4 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-block.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336772   178.3    16785850   896.5    58645897  3132.1
Vcells 6471208909 49371.5 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemBlockDiff','DemBlock_year1','RepBlockDiff','RepBlock_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345489   178.7    13428680   717.2    58645897  3132.1
Vcells 7145061169 54512.5 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemBlockDems = summary(felm(DemDiff ~ DemBlockDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockReps = summary(felm(DemDiff ~ DemBlockDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockOths = summary(felm(DemDiff ~ DemBlockDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockDems = summary(felm(RepDiff ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockReps = summary(felm(RepDiff ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockOths = summary(felm(RepDiff ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemBlockDems, ModelDemBlockReps, ModelDemBlockOths, 
+        ModelRepBlockDems, ModelRepBlockReps, ModelRepBlockOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-block.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335299   178.2    18680287   997.7    58645897  3132.1
Vcells 6569533879 50121.6 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-full-knn.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336781   178.3    14944230   798.2    58645897  3132.1
Vcells 6569533579 50121.6 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff', 'RepSpExpDiff','DemSpExp_year1','RepSpExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345498   178.7    11955384   638.5    58645897  3132.1
Vcells 7243385838 55262.7 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-full-knn.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335311   178.2    20004322  1068.4    58645897  3132.1
Vcells 6667858606 50871.8 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-dr.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336699   178.2    16003458   854.7    58645897  3132.1
Vcells 6667857040 50871.8 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DRSpExpDiff_nohh','DRSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345410   178.7    12802767   683.8    58645897  3132.1
Vcells 7288385294 55606.0 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables 
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemSpExpDems = summary(felm(DemDiff ~ DRSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpReps = summary(felm(DemDiff ~ DRSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpOths = summary(felm(DemDiff ~ DRSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpDems = summary(felm(RepDiff ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpReps = summary(felm(RepDiff ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpOths = summary(felm(RepDiff ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2012-2016-dr.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335320   178.2    14819188   791.5    58645897  3132.1
Vcells 6757989593 51559.4 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-k100.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336802   178.3    11855351   633.2    58645897  3132.1
Vcells 6757989345 51559.4 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh_k100', 'RepSpExpDiff_nohh_k100','DemSpExp_nohh_k100_year1','RepSpExp_nohh_k100_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345519   178.7     9484281   506.6    58645897  3132.1
Vcells 7431841604 56700.5 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k100 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k100+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k100+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-k100.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger  (Mb)    max used    (Mb)
Ncells    3335332   178.2    22973684  1227    58645897  3132.1
Vcells 6856314344 52309.6 12380529682 94456 12380342215 94454.6

> source('EST-current-results-2012-2016-k500.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336814   178.3    18378948   981.6    58645897  3132.1
Vcells 6856314084 52309.6 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh_k500', 'RepSpExpDiff_nohh_k500','DemSpExp_nohh_k500_year1','RepSpExp_nohh_k500_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345531   178.7    14703159   785.3    58645897  3132.1
Vcells 7530166343 57450.7 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables b
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k500 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k500+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k500+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-k500.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335344   178.2    20442126  1091.8    58645897  3132.1
Vcells 6954639079 53059.7 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-d1600.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336826   178.3    16353701   873.4    58645897  3132.1
Vcells 6954638820 53059.7 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh_d1600', 'RepSpExpDiff_nohh_d1600','DemSpExp_nohh_d1600_year1','RepSpExp_nohh_d1600_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345543   178.7    13082961   698.8    58645897  3132.1
Vcells 7628491079 58200.8 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_d1600 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_d1600+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_d1600+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-d1600.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335341   178.2    18202364   972.2    58645897  3132.1
Vcells 7052957463 53809.8 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-new-neighbors.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336945   178.3    14561892   777.7    58645897  3132.1
Vcells 7052964297 53809.9 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('net_new_dems','net_new_reps','DemDiff','RepDiff','DemExp_year1','RepExp_year1','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345674   178.7    11649514   622.2    58645897  3132.1
Vcells 7806802594 59561.2 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # by aspatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # convert net_new_dems/reps to proportion of 1000 nearest neighbors
> dems[,DemNewDiff:=net_new_dems/1000]

> reps[,DemNewDiff:=net_new_dems/1000]

> oths[,DemNewDiff:=net_new_dems/1000]

> dems[,RepNewDiff:=net_new_reps/1000]

> reps[,RepNewDiff:=net_new_reps/1000]

> oths[,RepNewDiff:=net_new_reps/1000]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemNewDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemNewDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemNewDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-new-neighbors.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335358   178.2    16220767   866.3    58645897  3132.1
Vcells 7163574917 54653.8 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2012-2016-no-bg-control.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336595   178.2    12976614   693.1    58645897  3132.1
Vcells 7163576773 54653.8 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345279   178.7    10381292   554.5    58645897  3132.1
Vcells 7597470947 57964.2 12380529682 94456.0 12380342215 94454.6

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-no-bg-controls.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3335325   178.2    20903712  1116.4    58645897  3132.1
Vcells 7225027781 55122.6 12380529682 94456.0 12380342215 94454.6

> source('EST-current-results-2016-2020-aspatial.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3336847   178.3    16722970   893.2    58645897  3132.1
Vcells 7225031794 55122.7 12380529682 94456.0 12380342215 94454.6

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemExpDiff','DemExp_year1','RepExpDiff','RepExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger    (Mb)    max used    (Mb)
Ncells    3345140   178.7    13378376   714.5    58645897  3132.1
Vcells 8082803692 61666.9 12380529682 94456.0 12380342215 94454.6

>   # construct grouping variables 
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemExpDems = summary(felm(DemDiff ~ DemExpDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemExpReps = summary(felm(DemDiff ~ DemExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemExpOths = summary(felm(DemDiff ~ DemExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepExpDems = summary(felm(RepDiff ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepExpReps = summary(felm(RepDiff ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepExpOths = summary(felm(RepDiff ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemExpDems, ModelDemExpReps, ModelDemExpOths, 
+        ModelRepExpDems, ModelRepExpReps, ModelRepExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-aspatial.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335353   178.2    22364896   1194.5    58645897   3132.1
Vcells 7334844622 55960.5 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-block-group.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336845   178.3    17891917    955.6    58645897   3132.1
Vcells 7334850742 55960.5 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemBlockGroupDiff','DemBlockGroup_year1','RepBlockGroupDiff','RepBlockGroup_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345574   178.7    14313534    764.5    58645897   3132.1
Vcells 8192624022 62504.8 14856715618 113347.8 14855081968 113335.3

>   # construct grouping variables 
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemBlockGroupDems = summary(felm(DemDiff ~ DemBlockGroupDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockGroupReps = summary(felm(DemDiff ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockGroupOths = summary(felm(DemDiff ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockGroupDems = summary(felm(RepDiff ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockGroupReps = summary(felm(RepDiff ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockGroupOths = summary(felm(RepDiff ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemBlockGroupDems, ModelDemBlockGroupReps, ModelDemBlockGroupOths, 
+        ModelRepBlockGroupDems, ModelRepBlockGroupReps, ModelRepBlockGroupOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-block-group.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335368   178.2    16559591    884.4    58645897   3132.1
Vcells 7444669905 56798.4 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-block.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336847   178.3    13247673    707.6    58645897   3132.1
Vcells 7444669654 56798.4 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemBlockDiff','DemBlock_year1','RepBlockDiff','RepBlock_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345576   178.7    10598139    566.1    58645897   3132.1
Vcells 8302442934 63342.7 14856715618 113347.8 14855081968 113335.3

>   # construct grouping variables based on age decile, gender, race, year1 treatment decile, and city
>   # we can drop files where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemBlockDems = summary(felm(DemDiff ~ DemBlockDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockReps = summary(felm(DemDiff ~ DemBlockDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemBlockOths = summary(felm(DemDiff ~ DemBlockDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockDems = summary(felm(RepDiff ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockReps = summary(felm(RepDiff ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepBlockOths = summary(felm(RepDiff ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemBlockDems, ModelDemBlockReps, ModelDemBlockOths, 
+        ModelRepBlockDems, ModelRepBlockReps, ModelRepBlockOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-block.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335368   178.2    21335377   1139.5    58645897   3132.1
Vcells 7554488817 57636.2 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-full-knn.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336847   178.3    17068302    911.6    58645897   3132.1
Vcells 7554488546 57636.2 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff','DemSpExp_year1','RepSpExpDiff','RepSpExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345576   178.7    13654642    729.3    58645897   3132.1
Vcells 8412261826 64180.5 14856715618 113347.8 14855081968 113335.3

>   # construct grouping variables 
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-full-knn.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335358   178.2    22823188   1218.9    58645897   3132.1
Vcells 7664303475 58474.0 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-dr.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336747   178.3    18258551    975.2    58645897   3132.1
Vcells 7664306153 58474.1 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DRSpExpDiff_nohh','DRSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                               'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                               'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

>   data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

>   data[countyfips=='',countyfips:=NA]

>   data[ZipCode=='',ZipCode:=NA]

>   data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

>   # split data
>   dems = data[Party_year1=='Democrat']

>   reps = data[Party_year1=='Republican']

>   oths = data[Party_year1=='Other']

>   rm(data)

>   gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345470   178.7    14606841    780.1    58645897   3132.1
Vcells 8454273747 64501.0 14856715618 113347.8 14855081968 113335.3

>   # construct grouping variables 
>   # we can drop rows where any of these are missing
>   
>   
>   dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

>   reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

>   dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   # spatial seg treatment
>   # democrat exposure
>   dems[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   reps[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   oths[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

>   #####
>   # make groups
>   # spatial seg treatment
>   dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

>   oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

>   # estimate models
>   
>   # spatial seg treatment
>   ModelDemSpExpDems = summary(felm(DemDiff ~ DRSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpReps = summary(felm(DemDiff ~ DRSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelDemSpExpOths = summary(felm(DemDiff ~ DRSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpDems = summary(felm(RepDiff ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpReps = summary(felm(RepDiff ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   ModelRepSpExpOths = summary(felm(RepDiff ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                      HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                      DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

>   save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+        ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+        
+        
+        file = paste0('results/current-results-2016-2020-dr.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335368   178.2    16897480    902.5    58645897   3132.1
Vcells 7764975049 59242.1 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-k100.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336838   178.3    13517984    722.0    58645897   3132.1
Vcells 7764974785 59242.1 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh_k100', 'RepSpExpDiff_nohh_k100','DemSpExp_nohh_k100_year1','RepSpExp_nohh_k100_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345567   178.7    10814388    577.6    58645897   3132.1
Vcells 8622748064 65786.4 14856715618 113347.8 14855081968 113335.3

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k100 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k100+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k100+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-k100.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335358   178.2    26151026   1396.7    58645897   3132.1
Vcells 7874789723 60079.9 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-k500.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336838   178.3    20920821   1117.3    58645897   3132.1
Vcells 7874793697 60080.0 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh_k500', 'RepSpExpDiff_nohh_k500','DemSpExp_nohh_k500_year1','RepSpExp_nohh_k500_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345567   178.7    16736657    893.9    58645897   3132.1
Vcells 8732566976 66624.2 14856715618 113347.8 14855081968 113335.3

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k500 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k500+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_k500+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-k500.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335368   178.2    23253234   1241.9    58645897   3132.1
Vcells 7984612889 60917.8 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-d1600.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336838   178.3    18602588    993.5    58645897   3132.1
Vcells 7984612615 60917.8 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh_d1600', 'RepSpExpDiff_nohh_d1600','DemSpExp_nohh_d1600_year1','RepSpExp_nohh_d1600_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345567   178.7    14882071    794.8    58645897   3132.1
Vcells 8842385894 67462.1 14856715618 113347.8 14855081968 113335.3

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_d1600 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_d1600+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_d1600+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-d1600.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335368   178.2    20689455   1105.0    58645897   3132.1
Vcells 8094431801 61755.7 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-new-neighbors.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336944   178.3    16551564    884.0    58645897   3132.1
Vcells 8094432253 61755.7 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('net_new_dems','net_new_reps','DemDiff','RepDiff','DemExp_year1','RepExp_year1','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345094   178.7    13241252    707.2    58645897   3132.1
Vcells 9053847659 69075.4 14856715618 113347.8 14855081968 113335.3

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # by aspatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems[,DemNewDiff:=net_new_dems/1000]

> reps[,DemNewDiff:=net_new_dems/1000]

> oths[,DemNewDiff:=net_new_dems/1000]

> dems[,RepNewDiff:=net_new_reps/1000]

> reps[,RepNewDiff:=net_new_reps/1000]

> oths[,RepNewDiff:=net_new_reps/1000]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemNewDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemNewDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemNewDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-new-neighbors.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335353   178.2    18421186    983.8    58645897   3132.1
Vcells 8217971740 62698.2 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-no-bg-control.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336595   178.2    14736949    787.1    58645897   3132.1
Vcells 8217975719 62698.2 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345291   178.7    11789560    629.7    58645897   3132.1
Vcells 8770623353 66914.6 14856715618 113347.8 14855081968 113335.3

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> ## output SDs
> 
> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-no-bg-controls.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335325   178.2    23707088   1266.1    58645897   3132.1
Vcells 8286610597 63221.9 14856715618 113347.8 14855081968 113335.3

> source('EST-current-results-2016-2020-race-imps.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336923   178.3    18965671   1012.9    58645897   3132.1
Vcells 8286615656 63221.9 14856715618 113347.8 14855081968 113335.3

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('ts.tsmr_p_white','DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,Race:=as.numeric(ts.tsmr_p_white)]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3345064   178.7    15172537    810.4    58645897   3132.1
Vcells 9178225401 70024.4 14856715618 113347.8 14855081968 113335.3

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> dems[,Race:=cut(Race,breaks=unique(as.numeric(quantile(Race, na.rm=T))),include.lowest=T)]

> reps[,Race:=cut(Race,breaks=unique(as.numeric(quantile(Race, na.rm=T))),include.lowest=T)]

> oths[,Race:=cut(Race,breaks=unique(as.numeric(quantile(Race, na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-race-imps.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3335361   178.2    25341192   1353.4    58645897   3132.1
Vcells 8401005371 64094.6 14856715618 113347.8 14856667575 113347.4

> source('TAB-alternative-effect-estimates.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> library(data.table)

> library(xtable)

> library(tidyr)

> library(dplyr)

> library(tibble)

> library(readr)

> files = paste0('results/',
+               c('current-results-2012-2016.Rdata',
+                 'current-results-2016-2020.Rdata',
+                 'current-results-2012-2016-pretrend.Rdata',
+                 'current-results-2016-2020-pretrend.Rdata',
+                 'current-results-2012-2016-full-knn.Rdata',
+                 'current-results-2016-2020-full-knn.Rdata',
+                 'current-results-2012-2016-dr.Rdata',
+                 'current-results-2016-2020-dr.Rdata',
+                 'current-results-2012-2016-d1600.Rdata',
+                 'current-results-2012-2016-k100.Rdata',
+                 'current-results-2012-2016-k500.Rdata',
+                 'current-results-2012-2016-block.Rdata',
+                 'current-results-2012-2016-block-group.Rdata',
+                 
+                 'current-results-2016-2020-d1600.Rdata',
+                 'current-results-2016-2020-k100.Rdata',
+                 'current-results-2016-2020-k500.Rdata',
+                 'current-results-2016-2020-block.Rdata',
+                 'current-results-2016-2020-block-group.Rdata',
+                 
+                 'current-results-2012-2016-aspatial.Rdata',
+                 'current-results-2016-2020-aspatial.Rdata',
+                 
+                 'current-results-2012-2016-new-neighbors.Rdata',
+                 'current-results-2016-2020-new-neighbors.Rdata',
+                 
+                 'current-results-2012-2016-no-bg-controls.Rdata',
+                 'current-results-2016-2020-no-bg-controls.Rdata',
+                 
+                 'current-results-2016-2020-race-imps.Rdata'
+                 )
+                )

> results = rbindlist(lapply(files, FUN=function(file){
+   
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+     #print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+     
+     
+     return(out)
+     
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+   l[grepl('2020',file),Year1:='2016']
+   l[grepl('2020',file),Year2:='2020'] 
+   l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+   l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+   l[,file:=file]
+   
+   return(l)
+ }))

> ### 2012-2016
> 
> t = data.table(Treatment = c('Main', 'Main',
+                              'Pre-trend', 'Pre-trend',
+                              'Aspatial', 'Aspatial',
+                              'Dem. Ratio', 'Dem. Ratio',
+                              'Include Same Household', 'Include Same Household',
+                              '100 Neighbors', '100 Neighbors',
+                              '500 Neighbors', '500 Neighbors',
+                              'Mile Radius', 'Mile Radius',
+                              'New Neighbors', 'New Neighbors',
+                              'Census Block', 'Census Block',
+                              'Census Block Group', 'Census Block Group',
+                              'No BG Controls', 'No BG Controls'),
+                
+                Stat = rep(c('Coef','SE'),12),
+                
+                DED = c(
+                 results[Covariate=='DemSpExpDiff_nohh' & 
+                                    file == 'results/current-results-2012-2016.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                                    file == 'results/current-results-2012-2016.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$SE,
+                  
+                 results[Covariate=='DemSpExpDiff_nohh' & 
+                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
+                           Model == 'ModelDemSpExpDems']$Estimate,
+                 results[Covariate=='DemSpExpDiff_nohh' & 
+                                   file == 'results/current-results-2012-2016-pretrend.Rdata' &
+                                   Model == 'ModelDemSpExpDems']$SE,
+                 
+                  results[Covariate=='DemExpDiff' & 
+                                    file == 'results/current-results-2012-2016-aspatial.Rdata' &
+                                    Model == 'ModelDemExpDems']$Estimate,
+                  results[Covariate=='DemExpDiff' & 
+                                    file == 'results/current-results-2012-2016-aspatial.Rdata' &
+                                    Model == 'ModelDemExpDems']$SE,
+                  
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                                    file == 'results/current-results-2012-2016-dr.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                                    file == 'results/current-results-2012-2016-dr.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                                    file == 'results/current-results-2012-2016-k100.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$Estimate,
+                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                           file == 'results/current-results-2012-2016-k100.Rdata' &
+                           Model == 'ModelDemSpExpDems']$SE,
+                  
+                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                           file == 'results/current-results-2012-2016-k500.Rdata' &
+                           Model == 'ModelDemSpExpDems']$Estimate,
+                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                           file == 'results/current-results-2012-2016-k500.Rdata' &
+                           Model == 'ModelDemSpExpDems']$SE,
+                  
+                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                           file == 'results/current-results-2012-2016-d1600.Rdata' &
+                           Model == 'ModelDemSpExpDems']$Estimate,
+                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                           file == 'results/current-results-2012-2016-d1600.Rdata' &
+                           Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemNewDiff' & 
+                                    file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemNewDiff' & 
+                            file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
+                                    Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemBlockDiff' & 
+                                    file == 'results/current-results-2012-2016-block.Rdata' &
+                                    Model == 'ModelDemBlockDems']$Estimate,
+                  results[Covariate=='DemBlockDiff' & 
+                            file == 'results/current-results-2012-2016-block.Rdata' &
+                                    Model == 'ModelDemBlockDems']$SE,
+                  
+                  results[Covariate=='DemBlockGroupDiff' & 
+                            file == 'results/current-results-2012-2016-block-group.Rdata' &
+                                    Model == 'ModelDemBlockGroupDems']$Estimate,
+                  results[Covariate=='DemBlockGroupDiff' & 
+                            file == 'results/current-results-2012-2016-block-group.Rdata' &
+                                    Model == 'ModelDemBlockGroupDems']$SE,
+                 
+                 results[Covariate=='DemSpExpDiff_nohh' & 
+                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
+                           Model == 'ModelDemSpExpDems']$Estimate,
+                 results[Covariate=='DemSpExpDiff_nohh' & 
+                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
+                           Model == 'ModelDemSpExpDems']$SE
+                  
+                  
+                  ),
+                
+                DER = c(
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2012-2016.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2012-2016.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2012-2016-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2012-2016-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/current-results-2012-2016-aspatial.Rdata' &
+                            Model == 'ModelDemExpReps']$Estimate,
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/current-results-2012-2016-aspatial.Rdata' &
+                            Model == 'ModelDemExpReps']$SE,
+                  
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/current-results-2012-2016-dr.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/current-results-2012-2016-dr.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/current-results-2012-2016-k100.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/current-results-2012-2016-k100.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/current-results-2012-2016-k500.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/current-results-2012-2016-k500.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/current-results-2012-2016-d1600.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'resul .... [TRUNCATED] 

> t[,DED:=ifelse(Stat=='Coef',round(DED,3),paste0('(',round(DED,3),')'))]

> t[,DER:=ifelse(Stat=='Coef',round(DER,3),paste0('(',round(DER,3),')'))]

> t[,DEI:=ifelse(Stat=='Coef',round(DEI,3),paste0('(',round(DEI,3),')'))]

> t[,RED:=ifelse(Stat=='Coef',round(RED,3),paste0('(',round(RED,3),')'))]

> t[,RER:=ifelse(Stat=='Coef',round(RER,3),paste0('(',round(RER,3),')'))]

> t[,REI:=ifelse(Stat=='Coef',round(REI,3),paste0('(',round(REI,3),')'))]

> t[Stat=='SE',Treatment:='']

> t[,Stat:=NULL]

> out = gt(t) %>%
+   cols_label(
+     `DED` = 'Democrats',
+     `DER` = 'Republicans',
+     `DEI` = 'Non-Partisans',
+     `RED` = 'Democrats',
+     `RER` = 'Republicans',
+     `REI` = 'Non-Partisans'
+   )%>%
+   tab_spanner(label = 'DV: Democratic Registration',columns = 2:4)%>%
+   tab_spanner(label = 'DV: Republican Registration',columns = 5:7)%>%
+   tab_spanner(label = 'Current effects: 2012-2016',columns = 2:7)%>%
+   
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')

> write_file(out, 'tables/TabS12a.tex')  

> ### 2016-2020
> 
> 
> t = data.table(Treatment = c('Main', 'Main',
+                              'Pre-trend', 'Pre-trend',
+                              'Aspatial', 'Aspatial',
+                              'Dem. Ratio', 'Dem. Ratio',
+                              'Include Same Household', 'Include Same Household',
+                              '100 Neighbors', '100 Neighbors',
+                              '500 Neighbors', '500 Neighbors',
+                              'Mile Radius', 'Mile Radius',
+                              'New Neighbors', 'New Neighbors',
+                              'Census Block', 'Census Block',
+                              'Census Block Group', 'Census Block Group',
+                              'No BG Controls', 'No BG Controls',
+                              'Race Posteriors','Race Posteriors'),
+                
+                Stat = rep(c('Coef','SE'),13),
+                
+                DED = c(
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/current-results-2016-2020-aspatial.Rdata' &
+                            Model == 'ModelDemExpDems']$Estimate,
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/current-results-2016-2020-aspatial.Rdata' &
+                            Model == 'ModelDemExpDems']$SE,
+                  
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-dr.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-dr.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2016-2020-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2016-2020-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/current-results-2016-2020-k100.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/current-results-2016-2020-k100.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/current-results-2016-2020-k500.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/current-results-2016-2020-k500.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/current-results-2016-2020-d1600.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/current-results-2016-2020-d1600.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemNewDiff' & 
+                            file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemNewDiff' & 
+                            file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemBlockDiff' & 
+                            file == 'results/current-results-2016-2020-block.Rdata' &
+                            Model == 'ModelDemBlockDems']$Estimate,
+                  results[Covariate=='DemBlockDiff' & 
+                            file == 'results/current-results-2016-2020-block.Rdata' &
+                            Model == 'ModelDemBlockDems']$SE,
+                  
+                  results[Covariate=='DemBlockGroupDiff' & 
+                            file == 'results/current-results-2016-2020-block-group.Rdata' &
+                            Model == 'ModelDemBlockGroupDems']$Estimate,
+                  results[Covariate=='DemBlockGroupDiff' & 
+                            file == 'results/current-results-2016-2020-block-group.Rdata' &
+                            Model == 'ModelDemBlockGroupDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-race-imps.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-race-imps.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE
+                  
+                  
+                ),
+                
+                DER = c(
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/current-results-2016-2020-aspatial.Rdata' &
+                            Model == 'ModelDemExpReps']$Estimate,
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/current-results-2016-2020-aspatial.Rdata' &
+                            Model == 'ModelDemExpReps']$SE,
+                  
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-dr.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/current-results-2016-2020-dr.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2016-2020-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/current-results-2016-2020-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/current-results-2016-2020-k100.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/current-results-2016-2020-k100.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/current-results-2016-2020-k500.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/current-results-2016-2020-k500.Rdata' &
+                            Model == 'ModelDemSpExpR .... [TRUNCATED] 

> t[,DED:=ifelse(Stat=='Coef',round(DED,3),paste0('(',round(DED,3),')'))]

> t[,DER:=ifelse(Stat=='Coef',round(DER,3),paste0('(',round(DER,3),')'))]

> t[,DEI:=ifelse(Stat=='Coef',round(DEI,3),paste0('(',round(DEI,3),')'))]

> t[,RED:=ifelse(Stat=='Coef',round(RED,3),paste0('(',round(RED,3),')'))]

> t[,RER:=ifelse(Stat=='Coef',round(RER,3),paste0('(',round(RER,3),')'))]

> t[,REI:=ifelse(Stat=='Coef',round(REI,3),paste0('(',round(REI,3),')'))]

> t[Stat=='SE',Treatment:='']

> t[,Stat:=NULL]

> out = gt(t) %>%
+   cols_label(
+     `DED` = 'Democrats',
+     `DER` = 'Republicans',
+     `DEI` = 'Non-Partisans',
+     `RED` = 'Democrats',
+     `RER` = 'Republicans',
+     `REI` = 'Non-Partisans'
+   )%>%
+   tab_spanner(label = 'DV: Democratic Registration',columns = 2:4)%>%
+   tab_spanner(label = 'DV: Republican Registration',columns = 5:7)%>%
+   tab_spanner(label = 'Current effects: 2016-2020',columns = 2:7)%>%
+   
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')

> write_file(out, 'tables/TabS12b.tex')  

> toc()
44070.696 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3340603   178.5    24228509   1294.0    58645897   3132.1
Vcells 8402770861 64108.1 14856715618 113347.8 14856667575 113347.4

> # Table S13
> tic() # time scripts for each figure 

> # If figure 4 is not yet created, run commented out scripts below
> #source('EST-future-results-2012-2016.R', echo = T,  max.deparse.length=10000)
> .... [TRUNCATED] 

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338194   178.3    19382808   1035.2    58645897   3132.1
Vcells 8401092863 64095.3 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemExpDiff', 'RepExpDiff','DemExp_year1','RepExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3346988   178.8    15506247    828.2    58645897   3132.1
Vcells 9143905449 69762.5 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemExpDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-aspatial.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336627   178.2    17933596    957.8    58645897   3132.1
Vcells 8503511443 64876.7 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-block-group.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338279   178.3    14346877    766.3    58645897   3132.1
Vcells 8503514678 64876.7 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemBlockGroupDiff','DemBlockGroup_year1','RepBlockGroupDiff','RepBlockGroup_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347067   178.8    11477502    613.0    58645897   3132.1
Vcells 9246327116 70543.9 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemBlockGroup_year1,breaks=unique(as.numeric(quantile(DemBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepBlockGroup_year1,breaks=unique(as.numeric(quantile(RepBlockGroup_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemBlockGroupDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemBlockGroupDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepBlockGroupDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-block-group.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336702   178.2    15982978    853.6    58645897   3132.1
Vcells 8605933085 65658.1 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-block.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338354   178.3    12786383    682.9    58645897   3132.1
Vcells 8605936282 65658.1 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemBlockDiff','DemBlock_year1','RepBlockDiff','RepBlock_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347142   178.8    10229107    546.3    58645897   3132.1
Vcells 9348748720 71325.3 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemBlock_year1,breaks=unique(as.numeric(quantile(DemBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepBlock_year1,breaks=unique(as.numeric(quantile(RepBlock_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemBlockDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemBlockDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemBlockDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepBlockDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-block.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336777   178.3    20600762   1100.2    58645897   3132.1
Vcells 8708354703 66439.5 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-full-knn.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338429   178.3    16480610    880.2    58645897   3132.1
Vcells 8708357900 66439.5 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff', 'RepSpExpDiff','DemSpExp_year1','RepSpExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347217   178.8    13184488    704.2    58645897   3132.1
Vcells 9451170338 72106.8 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_year1,breaks=unique(as.numeric(quantile(DemSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_year1,breaks=unique(as.numeric(quantile(RepSpExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-full-knn.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336852   178.3    18342716    979.7    58645897   3132.1
Vcells 8810776321 67220.9 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-dr.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338405   178.3    14674173    783.7    58645897   3132.1
Vcells 8810778212 67221.0 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DRSpExpDiff_nohh','DRSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347187   178.8    11739339    627.0    58645897   3132.1
Vcells 9501990178 72494.5 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DRSpDecile:=cut(DRSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DRSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, DRSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DRSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DRSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DRSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ DRSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-dr.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336923   178.3    13594118    726.1    58645897   3132.1
Vcells 8905004198 67939.8 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-k100.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338575   178.3    10875295    580.9    58645897   3132.1
Vcells 8905007431 67939.9 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh_k100', 'RepSpExpDiff_nohh_k100','DemSpExp_nohh_k100_year1','RepSpExp_nohh_k100_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347363   178.8    10875295    580.9    58645897   3132.1
Vcells 9647819869 73607.1 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_k100_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k100_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_k100 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_k100+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_k100+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_k100+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-k100.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3336998   178.3    21887100   1168.9    58645897   3132.1
Vcells 9007425829 68721.3 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-k500.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338650   178.4    17509680    935.2    58645897   3132.1
Vcells 9007429049 68721.3 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh_k500', 'RepSpExpDiff_nohh_k500','DemSpExp_nohh_k500_year1','RepSpExp_nohh_k500_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347438   178.8    14007744    748.1    58645897   3132.1
Vcells 9750241487 74388.5 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_k500_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_k500_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_k500 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_k500+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_k500+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_k500+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-k500.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3337305   178.3    19480784   1040.4    58645897   3132.1
Vcells 9109850085 69502.7 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-d1600.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338952   178.4    15584628    832.4    58645897   3132.1
Vcells 9109851191 69502.7 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh_d1600', 'RepSpExpDiff_nohh_d1600','DemSpExp_nohh_d1600_year1','RepSpExp_nohh_d1600_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347740   178.8    12467703    665.9    58645897   3132.1
Vcells 9852663629 75169.9 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_d1600_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_d1600_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_d1600 + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_d1600+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_d1600+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_d1600+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-d1600.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3337414   178.3    20854197   1113.8    58645897   3132.1
Vcells 9212269647 70284.1 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-new-neighbors.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339127   178.4    16683358    891.0    58645897   3132.1
Vcells 9212273456 70284.1 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('net_new_dems','net_new_reps','DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemExp_year1','RepExp_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347915   178.8    13346687    712.8    58645897   3132.1
Vcells 9929285696 75754.5 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # aspatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemExp_year1,breaks=unique(as.numeric(quantile(DemExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepExp_year1,breaks=unique(as.numeric(quantile(RepExp_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> dems[,DemNewDiff:=net_new_dems/1000]

> reps[,DemNewDiff:=net_new_dems/1000]

> oths[,DemNewDiff:=net_new_dems/1000]

> dems[,RepNewDiff:=net_new_reps/1000]

> reps[,RepNewDiff:=net_new_reps/1000]

> oths[,RepNewDiff:=net_new_reps/1000]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemNewDiff + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemNewDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemNewDiff+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepNewDiff+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-new-neighbors.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3337450   178.3    15445783    824.9    58645897   3132.1
Vcells 9310594336 71034.2 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-no-bg-control.R', echo = T, max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338860   178.4    12356627    660.0    58645897   3132.1
Vcells 9310595487 71034.3 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347615   178.8     9885302    528.0    58645897   3132.1
Vcells 9821205725 74929.9 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-no-bg-controls.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3337495   178.3    19916365   1063.7    58645897   3132.1
Vcells 9376154662 71534.4 14856715618 113347.8 14856667575 113347.4

> source('TAB-alternative-future-effect-estimates.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> library(data.table)

> library(xtable)

> library(tidyr)

> library(dplyr)

> library(tibble)

> library(readr)

> files = paste0('results/',
+               c('future-results-2012-2016.Rdata',
+                
+                 'future-results-2012-2016-pretrend.Rdata',
+                 
+                 'future-results-2012-2016-full-knn.Rdata',
+                
+                 'future-results-2012-2016-dr.Rdata',
+                 
+                 'future-results-2012-2016-d1600.Rdata',
+                 'future-results-2012-2016-k100.Rdata',
+                 'future-results-2012-2016-k500.Rdata',
+                 'future-results-2012-2016-block.Rdata',
+                 'future-results-2012-2016-block-group.Rdata',
+                 
+                 
+                 
+                 'future-results-2012-2016-aspatial.Rdata',
+                 'future-results-2012-2016-no-bg-controls.Rdata',
+                 
+                 
+                 'future-results-2012-2016-new-neighbors.Rdata'
+                
+                 )
+                )

> results = rbindlist(lapply(files, FUN=function(file){
+   
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+     # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+     
+     
+     return(out)
+     
+   }))
+   
+   l[grepl('2008',file),Year1:='2008']
+   l[grepl('2008',file),Year2:='2012']
+   l[grepl('2020',file),Year1:='2016']
+   l[grepl('2020',file),Year2:='2020'] 
+   l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
+   l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
+   l[,file:=file]
+   
+   return(l)
+ }))

> ### 2012-2016
> 
> t = data.table(Treatment = c('Main', 'Main',
+                              'Pretrend', 'Pretrend',
+                              'Aspatial', 'Aspatial',
+                              'Dem. Ratio', 'Dem. Ratio',
+                              'Include Same Household', 'Include Same Households',
+                              '100 Neighbors', '100 Neighbors',
+                              '500 Neighbors', '500 Neighbors',
+                              'Mile Radius', 'Mile Radius',
+                              'New Neighbors', 'New Neighbors',
+                              'Census Block', 'Census Block',
+                              'Census Block Group', 'Census Block Group',
+                              'No Census controls', 'No Census controls'),
+                
+                
+                Stat = rep(c('Coef','SE'),12),
+                
+                DED = c(
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/future-results-2012-2016-aspatial.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/future-results-2012-2016-aspatial.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-dr.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-dr.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/future-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/future-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/future-results-2012-2016-k100.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/future-results-2012-2016-k100.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/future-results-2012-2016-k500.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/future-results-2012-2016-k500.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/future-results-2012-2016-d1600.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/future-results-2012-2016-d1600.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemNewDiff' & 
+                            file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemNewDiff' & 
+                            file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemBlockDiff' & 
+                            file == 'results/future-results-2012-2016-block.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemBlockDiff' & 
+                            file == 'results/future-results-2012-2016-block.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemBlockGroupDiff' & 
+                            file == 'results/future-results-2012-2016-block-group.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemBlockGroupDiff' & 
+                            file == 'results/future-results-2012-2016-block-group.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
+                            Model == 'ModelDemSpExpDems']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
+                            Model == 'ModelDemSpExpDems']$SE
+                  
+                  
+                ),
+                
+                DER = c(
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-pretrend.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/future-results-2012-2016-aspatial.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemExpDiff' & 
+                            file == 'results/future-results-2012-2016-aspatial.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-dr.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DRSpExpDiff_nohh' & 
+                            file == 'results/future-results-2012-2016-dr.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/future-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff' & 
+                            file == 'results/future-results-2012-2016-full-knn.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/future-results-2012-2016-k100.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k100' & 
+                            file == 'results/future-results-2012-2016-k100.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/future-results-2012-2016-k500.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_k500' & 
+                            file == 'results/future-results-2012-2016-k500.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/future-results-2012-2016-d1600.Rdata' &
+                            Model == 'ModelDemSpExpReps']$Estimate,
+                  results[Covariate=='DemSpExpDiff_nohh_d1600' & 
+                            file == 'results/future-results-2012-2016-d1600.Rdata' &
+                            Model == 'ModelDemSpExpReps']$SE,
+                  
+                  results[Covariate=='DemNewDiff' & 
 .... [TRUNCATED] 

> t[,DED:=ifelse(Stat=='Coef',round(DED,3),paste0('(',round(DED,3),')'))]

> t[,DER:=ifelse(Stat=='Coef',round(DER,3),paste0('(',round(DER,3),')'))]

> t[,DEI:=ifelse(Stat=='Coef',round(DEI,3),paste0('(',round(DEI,3),')'))]

> t[,RED:=ifelse(Stat=='Coef',round(RED,3),paste0('(',round(RED,3),')'))]

> t[,RER:=ifelse(Stat=='Coef',round(RER,3),paste0('(',round(RER,3),')'))]

> t[,REI:=ifelse(Stat=='Coef',round(REI,3),paste0('(',round(REI,3),')'))]

> t[Stat=='SE',Treatment:='']

> t[,Stat:=NULL]

> out = gt(t) %>%
+   cols_label(
+     `DED` = 'Democrats',
+     `DER` = 'Republicans',
+     `DEI` = 'Non-Partisans',
+     `RED` = 'Democrats',
+     `RER` = 'Republicans',
+     `REI` = 'Non-Partisans'
+   )%>%
+   tab_spanner(label = 'DV: Democratic Registration',columns = 2:4)%>%
+   tab_spanner(label = 'DV: Republican Registration',columns = 5:7)%>%
+   tab_spanner(label = 'Downstream effects: 2012-2016 exposure change on 2016-2020 party switching',columns = 2:7)%>%
+   
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')

> write_file(out, 'tables/TabS13.tex')  

> toc()
13146.314 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338459   178.3    13132710    701.4    58645897   3132.1
Vcells 9376198630 71534.8 14856715618 113347.8 14856667575 113347.4

> # Figure S11 
> tic() # time scripts for each figure

> source('EST-future-results-2008-2012-alt-1.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339916   178.4    10506168    561.1    58645897   3132.1
Vcells 9376184115 71534.7 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread(paste0('panel-analysis-2008-2012.csv.gz'),select = c('DemDiff','Party_2016','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2012:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2012:='Republican']

> data[is.na(Party_2012), Party_2012:='Other']

> data[Party_2016=='',Party_2016:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2016=='Democrat')-as.numeric(Party_2012=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2016=='Republican')-as.numeric(Party_2012=='Republican')]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican' ]

> oths = data[Party_year1=='Other' ]

> rm(data)

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3347136   178.8    10506168    561.1    58645897   3132.1
Vcells 9928616088 75749.4 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2008-2012-alternative-subsets-1.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338377   178.3    14640206    781.9    58645897   3132.1
Vcells 9467602865 72232.1 14856715618 113347.8 14856667575 113347.4

> source('EST-future-results-2012-2016-alt-1.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3340020   178.4    11712165    625.5    58645897   3132.1
Vcells 9467608057 72232.2 14856715618 113347.8 14856667575 113347.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' ]

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other' ]

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3348154   178.9     9369732    500.4    58645897   3132.1
Vcells 10235749219 78092.6 14856715618 113347.8 14856667575 113347.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-alternative-subsets-1.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338456   178.3    18890042   1008.9    58645897   3132.1
Vcells 9570022385 73013.5 17828138741 136018.0 17827838338 136015.7

> source('EST-future-results-2008-2012-alt-2.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339995   178.4    15112034    807.1    58645897   3132.1
Vcells 9570028757 73013.6 17828138741 136018.0 17827838338 136015.7

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread(paste0('panel-analysis-2008-2012.csv.gz'),select = c('DemDiff','Party_2016','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2012:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2012:='Republican']

> data[is.na(Party_2012), Party_2012:='Other']

> data[Party_2016=='',Party_2016:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2016=='Democrat')-as.numeric(Party_year1=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2016=='Republican')-as.numeric(Party_year1=='Republican')]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican' ]

> oths = data[Party_year1=='Other' ]

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3347215   178.8    12089628    645.7    58645897   3132.1
Vcells 10122460730 77228.3 17828138741 136018.0 17827838338 136015.7

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2008-2012-alternative-subsets-2.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338452   178.3    13997651    747.6    58645897   3132.1
Vcells 9661447479 73711.0 17828138741 136018.0 17827838338 136015.7

> source('EST-future-results-2012-2016-alt-2.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3340095   178.4    11198121    598.1    58645897   3132.1
Vcells 9661452671 73711.1 17828138741 136018.0 17827838338 136015.7

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff'))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_year2-hh.r.adj_year1]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_year1=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_year1=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' ]

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other' ]

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3348885   178.9    11198121    598.1    58645897   3132.1
Vcells 10429687500 79572.3 17828138741 136018.0 17827838338 136015.7

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-alternative-subsets-2.Rdata'))

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3338531   178.3    18748114   1001.3    58645897   3132.1
Vcells 9763866999 74492.4 17828138741 136018.0 17827838338 136015.7

> source('FIG-future-results-alternative-subsets.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339411   178.4    14998492    801.1    58645897   3132.1
Vcells 9763865368 74492.4 17828138741 136018.0 17827838338 136015.7

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c('future-results-2008-2012.Rdata',
+                                         'future-results-2012-2016.Rdata',
+                                         
+                                         'future-results-2008-2012-alternative-subsets-1.Rdata',
+                                         'future-results-2012-2016-alternative-subsets-1.Rdata',
+                                         'future-results-2008-2012-alternative-subsets-2.Rdata',
+                                         'future-results-2012-2016-alternative-subsets-2.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+ l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
+                     grepl('2012-2016',file)~ '2012-2016')]  
+ l[,file:=file]
+ l[,Sample:=ifelse(grepl('-subsets-1',file),'Year 1 subsets',ifelse(grepl('-subsets-2',file),'Year 3 - Year 1 party switching' ,'Main'))]
+ l[,Year1:=substr(Years,1,4)]
+ 
+ return(l)
+ }))

> #results[,Sample:=factor(Sample,levels=c('Main','Alternative'))]
> 
> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh','DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results, aes(y = Estimate, x = Subset, color = Subset, shape = Sample))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'), strip.text.y = element_text(size=12,angle=0),legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/FigS11.png',dpi=300, width = 11, height = 8, units = 'in')

> toc()
4044.693 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339440   178.4    15464496    825.9    58645897   3132.1
Vcells 9763884024 74492.6 17828138741 136018.0 17827838338 136015.7

> # Table S14
> tic() # time scripts for each figure

> source('TAB-survey-population-comparison.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339839   178.4    12371597    660.8    58645897   3132.1
Vcells 9763871614 74492.5 17828138741 136018.0 17827838338 136015.7

> library(dplyr)

> library(tidyr)

> library(gt)

> library(stringr)

> library(tibble)

> library(readr)

> library(data.table)

> s = fread('survey-population-comparison.csv.gz')
|--------------------------------------------------|
|==================================================|

> means = s[,list(
+   Democrat = weighted.mean(Dem,w=w,na.rm=T),
+   Married = weighted.mean(Married,w=w, na.rm=T),
+   Republican = weighted.mean(Rep,w=w,na.rm=T),
+   White = weighted.mean(White,w=w,na.rm=T),
+   Black = weighted.mean(Black,w=w,na.rm=T),
+   Hispanic=weighted.mean(Hispanic,w=w,na.rm=T),
+   Asian = weighted.mean(Asian, w=w,na.rm=T),
+   Female = weighted.mean(Female,w=w,na.rm=T),
+   Age = weighted.mean(Age,w=w,na.rm=T),
+   `Democratic Exposure` = weighted.mean(DemSpExp_nohh,w=w,na.rm=T),
+   `Republican Exposure` = weighted.mean(RepSpExp_nohh,w=w,na.rm=T), 
+  
+   `Block Group White` = weighted.mean(WhiteBlockGroup,w=w,na.rm=T),
+   `Block Group Registered` = weighted.mean(RegsBlockGroup,w=w,na.rm=T),
+   `Block Group Median Age`=weighted.mean(AgeBlockGroup,w=w,na.rm=T),
+   `Block Group Median Household Income` = weighted.mean(HHIncomeBlockGroup,w=w,na.rm=T),
+   `Block Group Homeowner` = weighted.mean(HomeownerBlockGroup,w=w,na.rm=T),
+   `Block Group Median Year House Built` = weighted.mean(YearBuiltBlockGroup,w=w,na.rm=T),
+   `Block Group Drive to Work` = weighted.mean(DriveWorkBlockGroup,w=w,na.rm=T),
+   `Block Group Median House Value` = weighted.mean(HouseValueBlockGroup,w=w,na.rm=T),
+   `Vote 2016 General`= weighted.mean(GenVote2016,w=w,na.rm=T),
+   
+   `Vote 2018 General`= weighted.mean(GenVote2018,w=w,na.rm=T)
+   
+ ) ,by='Status']

> t = as_tibble(means)%>%
+   pivot_longer(Democrat:`Vote 2018 General`)%>%
+   pivot_wider(names_from='Status')

> out = t %>%
+   gt %>%
+   fmt_number(columns = 2:4, decimals = 3)%>%
+ 
+   as_latex()%>%
+   as.character()%>%
+   str_replace_all('longtable','tabular')

> write_file(out, 'tables/TabS14.tex')

> toc()
252.813 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3339472   178.4     9897278    528.6    58645897   3132.1
Vcells 9898063853 75516.3 17828138741 136018.0 17827838338 136015.7

> # Table S15 and S16 and S17
> tic() # time scripts for each figure

> # if Table 2 not already generated run commented out scripst below
> #source('EST-survey-analysis-weights.R', echo = T,  max.deparse.length=10000)
> .... [TRUNCATED] 

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3340864   178.5     9897278    528.6    58645897   3132.1
Vcells 9898070873 75516.3 17828138741 136018.0 17827838338 136015.7

> ## SURVEY RESULT TABLES
> library(modelsummary)

> library(dplyr)

> library(tidyr)

> library(stringr)

> library(tibble)

> library(readr)

> library(gt)

> library(data.table)

> library(gt)

> #gc()
> 
> load('results/survey-results-no-weights.Rdata')

> load('results/survey-results-weights.Rdata')

> source('felm-summary-table.R')

> vars = c('Dem Exp', 'HH Dem', 'Dem Exp * Dem', 'Rep Exp', 'HH Rep' ,'Rep Exp * Rep',
+          'BG White', 'BG Age', 'BG Registered', 'BG Med. HH Income', 'BG College', 'BG Homeowner', 'BG Year Built',
+          'BG Drive Work', 'BG Unemployed', 'BG Med. House Value', 'Asian','Black', 'Hispanic', 'White', 'Age','Male','Liberalism', 'Married'
+ , 'College', 'Years Residence','Party7'        )

> names(vars)= c('DemSpExp_nohh', 'hh.d.adj','DemSpExp_nohh:Democrat','RepSpExp_nohh','hh.r.adj', 'RepSpExp_nohh:Republican' ,
+                'WhiteBlockGroup', 'AgeBlockGroup', 'RegsBlockGroup', 'HHIncomeBlockGroup', 'CollegeBlockGroup', 'HomeownerBlockGroup',
+                'YearBuilBlockGroup', 'DriveWorkBlockGroup', 'EmplBlockGroup','HouseValueBlockGroup', 'RaceAsian','RaceBlack','RaceHispanic','RaceWhite',
+                'Age','Male','Liberalism','Married','College','YearsResidence','Party7')

> l = list(w1DemSpExp, m1DemSpExp,
+          w1RepSpExp, m1RepSpExp,
+          w2DemSpExp, m2DemSpExp,
+          w2RepSpExp, m2RepSpExp,
+          w5DemSpExp, m5DemSpExp,
+          w5RepSpExp, m5RepSpExp)

> names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)', '(7)', '(8)','(9)', '(10)', '(11)', '(12)')

> tab1 = felm.summary.table(summaries = l,
+                           coef_map = vars, output ='gt',
+                           model.names=names(l), fmt =2)

> tab1 %>%
+   
+   # column labels
+   tab_spanner(label = 'Neighbor PID', columns = 2:5) %>%
+   tab_spanner(label = 'Contact Dems', columns = 6:7) %>%
+   tab_spanner(label = 'Contact Reps', columns = 8:9) %>%
+   tab_spanner(label = 'Comfort', columns = 10:13) %>%
+   
+   
+   as_latex()%>%
+   as.character %>%
+   str_replace_all('longtable','tabular')%>%
+   str_replace_all(as.character(m1DemSpExp$N),format(m1DemSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w1DemSpExp$N),format(w1DemSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m1RepSpExp$N),format(m1RepSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w1RepSpExp$N),format(w1RepSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m2RepSpExp$N),format(m2RepSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m2DemSpExp$N),format(m2DemSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m5RepSpExp$N),format(m5RepSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m5DemSpExp$N),format(m5DemSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w2RepSpExp$N),format(w2RepSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w2DemSpExp$N),format(w2DemSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w5RepSpExp$N),format(w5RepSpExp$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w5DemSpExp$N),format(w5DemSpExp$N, trim=T, big.mark=','))%>%
+   
+   write_file('tables/TabS15.tex')

> ### comfort
> 
> 
> 
> vars = c('Comfort', 'Asian','Black', 'Hispanic', 'White', 'Age','Male', 'Married'
+          , 'College', 'Years Residence'       )

> names(vars)= c('Comfort', 'RaceAsian','RaceBlack','RaceHispanic','RaceWhite',
+                'Age','Male','Married','College','YearsResidence')

> l = list(w6DemSpExpDems, w6DemSpExpReps, w6DemSpExpOths,
+          w6RepSpExpDems, w6RepSpExpReps, w6RepSpExpOths)

> names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)')

> tab1 = felm.summary.table(summaries = l,
+                           coef_map = vars, output ='gt',
+                           model.names=names(l), fmt =3)

> tab1 %>%
+   
+   # column labels
+   tab_spanner(label = '2020 Democrat', columns = 2:4) %>%
+   tab_spanner(label = '2020 Republican', columns = 5:7) %>%
+   
+   
+   as_latex()%>%
+   as.character %>%
+   str_replace_all('longtable','tabular')%>%
+   str_replace_all(as.character(w6DemSpExpDems$N),format(w6DemSpExpDems$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w6DemSpExpReps$N),format(w6DemSpExpReps$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w6DemSpExpOths$N),format(w6DemSpExpOths$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w6RepSpExpDems$N),format(w6RepSpExpDems$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w6RepSpExpReps$N),format(w6RepSpExpReps$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(w6RepSpExpOths$N),format(w6RepSpExpOths$N, trim=T, big.mark=','))%>%
+   
+   write_file('tables/TabS16.tex')

> vars = c('Comfort', 'Asian','Black', 'Hispanic', 'White', 'Age','Male', 'Married'
+          , 'College', 'Years Residence'       )

> names(vars)= c('Comfort', 'RaceAsian','RaceBlack','RaceHispanic','RaceWhite',
+                'Age','Male','Married','College','YearsResidence')

> l = list(m6DemSpExpDems, m6DemSpExpReps, m6DemSpExpOths,
+          m6RepSpExpDems, m6RepSpExpReps, m6RepSpExpOths)

> names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)')

> tab1 = felm.summary.table(summaries = l,
+                           coef_map = vars, output ='gt',
+                           model.names=names(l), fmt =3)

> tab1 %>%
+   
+   # column labels
+   tab_spanner(label = '2020 Democrat', columns = 2:4) %>%
+   tab_spanner(label = '2020 Republican', columns = 5:7) %>%
+   
+   
+   as_latex()%>%
+   as.character %>%
+   str_replace_all('longtable','tabular')%>%
+   str_replace_all(as.character(m6DemSpExpDems$N),format(m6DemSpExpDems$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m6DemSpExpReps$N),format(m6DemSpExpReps$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m6DemSpExpOths$N),format(m6DemSpExpOths$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m6RepSpExpDems$N),format(m6RepSpExpDems$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m6RepSpExpReps$N),format(m6RepSpExpReps$N, trim=T, big.mark=','))%>%
+   str_replace_all(as.character(m6RepSpExpOths$N),format(m6RepSpExpOths$N, trim=T, big.mark=','))%>%
+   
+   write_file('tables/TabS17.tex')

> toc()
24.627 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3340833   178.5     9897278    528.6    58645897   3132.1
Vcells 9898116330 75516.7 17828138741 136018.0 17827838338 136015.7

> # Table S18 and S19
> tic() # time scripts for each figure

> source('TAB-alt-tables.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> library(xtable)

> load('dedup-comp.Rdata')

> # sample size table
> 
> 
> t = data.frame(
+   sample = c('2008-2012', '2012-2016','2016-2020', 'Unique voters\n(across samples)'),
+   n = c(n_official_1,n_official_2,n_official_3,n_official),
+   `n_alt` = c(n1,n2,n3,n)
+   
+ )

> t$perc = paste0(round(t$n_alt/t$n*100,2),'\\%')

> print(xtable(t), file = 'tables/TabS18.tex')

> # correlation table
> 
> 
> 
> t = data.frame(
+   party = c(rep('Democrat',8),rep('Republican',8)),
+   sample = rep(c('2008-2012', '2008-2012',
+              '2012-2016', '2012-2016','2012-2016',
+              '2016-2020', '2016-2020','2016-2020'),2),
+   stat = rep(c('2012', 'Delta 2008-2012',
+                '2012', '2016', 'Delta 2012-2016',
+                '2016', '2020', 'Delta 2016-2020'
+            ),2),
+   val = c(lev2.dem.cor1, diff.dem.cor1, 
+           lev1.dem.cor2, lev2.dem.cor2, diff.dem.cor2,
+           lev1.dem.cor3, lev2.dem.cor3, diff.dem.cor3,
+           
+           lev2.rep.cor1, diff.rep.cor1, 
+           lev1.rep.cor2, lev2.rep.cor2, diff.rep.cor2,
+           lev1.rep.cor3, lev2.rep.cor3, diff.rep.cor3
+           )
+   
+ )

> colnames(t) = c('Exposure', 'Sample', 'Stat', 'Correlation')

> print(xtable(t[,c('Sample', 'Exposure','Stat','Correlation')]), file = 'tables/TabS19.tex')

> toc()
0.063 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3340801   178.5     9897278    528.6    58645897   3132.1
Vcells 9898066443 75516.3 17828138741 136018.0 17827838338 136015.7

> # Figure S12
> tic() # time scripts for each figure

> source('FIG-party-switching-matrix-dedup.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3343127   178.6     9897278    528.6    58645897   3132.1
Vcells 9898086051 75516.5 17828138741 136018.0 17827838338 136015.7

> library(data.table)

> library(ggplot2)

> library(scales)

> source('theme_jake.R')

> data = rbindlist(lapply(c('panel-analysis-2008-2012.csv.gz',
+                           'panel-analysis-2012-2016.csv.gz',
+                           'panel-analysis-2016-2020.csv.gz'), FUN = function(x){
+                             d=fread(x, select = c('Unique_ID', 'voterbase_id','voterbase_id_year1','voterbase_id_year2',
+                                                   'Party_year1','DemDiff','RepDiff',
+                                                   'RepSpExp_nohh_alt_year1', 'RepSpExp_nohh_alt_year2',
+                                                   'DemSpExp_nohh_alt_year1', 'DemSpExp_nohh_alt_year2'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+                             
+                             if(grepl('2008-2012',x)){
+                               
+                               
+                               
+                               
+                               d = d[!is.na(RepSpExp_nohh_alt_year2)|!is.na(DemSpExp_nohh_alt_year2)]
+                             } else {
+                               d = d[(!is.na(RepSpExp_nohh_alt_year1)|!is.na(DemSpExp_nohh_alt_year1))&(!is.na(RepSpExp_nohh_alt_year2)|!is.na(DemSpExp_nohh_alt_year2))]
+                               
+                             } 
+                             
+                             
+                             return(d[,c('Party_year1','DemDiff','RepDiff','Years')])
+                           }))
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'),Party_year1:='Non-Partisan']

> data[DemDiff==1|(Party_year1=='Democrat' & DemDiff==0),Party_year2:='Democrat']

> data[RepDiff==1|(Party_year1=='Republican' & RepDiff==0),Party_year2:='Republican']

> data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']

> data[,DemDiff:=NULL]

> data[,RepDiff:=NULL]

> data[,Year1:=substr(Years,1,4)]

> data[,Year2:=substr(Years,6,9)]

> d = data[Years=='2016-2020']

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> library(dplyr)

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Party_year1:=paste0('2016\n',Party_year1)]

> p[,Party_year2:=paste0('2020\n',Party_year2)]

> g1=ggplot(p, aes(y = factor(Party_year1), x = factor(Party_year2), fill = color, label =prob))+
+   geom_tile(aes(alpha=a))+
+   scale_fill_manual(values=c('red','blue', 'grey','pink','lightblue','purple'))+
+   scale_alpha_manual(values = c(.5,.2,.1))+
+   geom_text( size = 8, color = 'black') +
+   geom_text(aes(label = paste0('(n = ',comma(Total),')')),nudge_y=-.15, size = 5, color = 'black') +
+   #scale_x_discrete(position = "top") +
+   coord_equal() +
+   geom_vline(xintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   geom_hline(yintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   ylab("") +  
+   coord_equal() +
+   xlab(paste(''))+
+   ylab(paste(''))+
+   theme_jake_tile2()+
+   theme(axis.text.y=element_text(angle=90,hjust=.5), text = element_text(size=24))
Coordinate system already present. Adding new coordinate system, which
will replace the existing one.

> p1620=p

> ## 2012-2016
> 
> d = data[Years=='2012-2016']

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3347034   178.8     9897278    528.6    58645897   3132.1
Vcells 10370951772 79124.1 17828138741 136018.0 17827838338 136015.7

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Party_year1:=paste0('2012\n',Party_year1)]

> p[,Party_year2:=paste0('2016\n',Party_year2)]

> g2=ggplot(p, aes(y = factor(Party_year1), x = factor(Party_year2), fill = color, label =prob))+
+   geom_tile(aes(alpha=a))+
+   scale_fill_manual(values=c('red','blue', 'grey','pink','lightblue','purple'))+
+   scale_alpha_manual(values = c(.5,.2,.1))+
+   geom_text( size = 8, color = 'black') +
+   geom_text(aes(label = paste0('(n = ',comma(Total),')')),nudge_y=-.15, size = 5, color = 'black') +
+   #scale_x_discrete(position = "top") +
+   coord_equal() +
+   geom_vline(xintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   geom_hline(yintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   ylab("") +  
+   coord_equal() +
+   xlab(paste(''))+
+   ylab(paste(''))+
+   theme_jake_tile2()+
+   theme(axis.text.y=element_text(angle=90,hjust=.5), text = element_text(size=24))
Coordinate system already present. Adding new coordinate system, which
will replace the existing one.

> p1216=p

> ## 2008 - 2012 
> d = data[Years=='2008-2012']

> p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]

> p[,N_1:=sum(Total),by = 'Party_year1']

> p[,prob:= round(Total/N_1,3)]

> p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
+                       Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
+                       Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
+                       Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
+                       Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
+                       
+ )]  

> p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']

> p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']

> p[Party_year1==Party_year2, a:='a']

> p[,Party_year1:=paste0('2008\n',Party_year1)]

> p[,Party_year2:=paste0('2012\n',Party_year2)]

> g3=ggplot(p, aes(y = factor(Party_year1), x = factor(Party_year2), fill = color, label =prob))+
+   geom_tile(aes(alpha=a))+
+   scale_fill_manual(values=c('red','blue', 'grey','pink','lightblue','purple'))+
+   scale_alpha_manual(values = c(.5,.2,.1))+
+   geom_text( size = 8, color = 'black') +
+   geom_text(aes(label = paste0('(n = ',comma(Total),')')),nudge_y=-.15, size = 5, color = 'black') +
+   #scale_x_discrete(position = "top") +
+   coord_equal() +
+   geom_vline(xintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   geom_hline(yintercept = 1:3 - 0.5, colour = "white", size = 1.5) +
+   ylab("") +  
+   coord_equal() +
+   xlab(paste(''))+
+   ylab(paste(''))+
+   theme_jake_tile2()+
+   theme(axis.text.y=element_text(angle=90,hjust=.5), text = element_text(size=24))
Coordinate system already present. Adding new coordinate system, which
will replace the existing one.

> library(cowplot)

> ggsave(plot = plot_grid(g3,g2,g1,nrow=1, labels = c('2008-2012', '2012-2016', '2016-2020'), hjust=-3.1), file = paste0('figures/FigS12.png'),width=18,height=6)

> toc()
1500.755 sec elapsed

> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3356983   179.3     9897278    528.6    58645897   3132.1
Vcells 9966883606 76041.3 17828138741 136018.0 17827838338 136015.7

> # Figure S13
> tic() # time scripts for each figure

> source('FIG-distributions-dedup.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
             used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells    3357727   179.4     9897278    528.6    58645897   3132.1
Vcells 9966780419 76040.6 17828138741 136018.0 17827838338 136015.7

> library(ggplot2)

> library(scales)

> library(data.table)

> library(lfe)

> library(stringr)

> data = rbindlist(lapply(c('panel-analysis-2008-2012.csv.gz',
+                           'panel-analysis-2012-2016.csv.gz',
+                           'panel-analysis-2016-2020.csv.gz'), FUN = function(x){
+   d=fread(x, select = c('Unique_ID','voterbase_id','voterbase_id_year1','voterbase_id_year2', 'DemSpExp_nohh_year1','RepSpExp_nohh_year1',
+                         'RepSpExp_nohh_alt_year1', 'RepSpExp_nohh_alt_year2',
+                         'DemSpExp_nohh_alt_year1', 'DemSpExp_nohh_alt_year2'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
+                           
+   
+   if(grepl('2008-2012',x)){
+     
+   
+     d[,DemSpExpDiff_nohh:=DemSpExp_nohh_alt_year2-DemSpExp_nohh_year1]
+     d[,RepSpExpDiff_nohh:=RepSpExp_nohh_alt_year2-RepSpExp_nohh_year1]
+     
+   } else {
+     
+     d[,DemSpExpDiff_nohh:=DemSpExp_nohh_alt_year2-DemSpExp_nohh_alt_year1]
+     d[,RepSpExpDiff_nohh:=RepSpExp_nohh_alt_year2-RepSpExp_nohh_alt_year1]
+     
+   } 
+     
+   return(d[,c('DemSpExpDiff_nohh','RepSpExpDiff_nohh','Years')])
+   
+   }))
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|
|--------------------------------------------------|
|==================================================|

> ## Make Plots 
> 
> # let's make a sample dataset just to make these plots
> 
> 
> holder1 = data[,c('DemSpExpDiff_nohh','Years')]

> holder1[,Exposure:=DemSpExpDiff_nohh][,Exposure.Type:='Exposure\nto Democrats']

> holder1= holder1[,c('Exposure','Exposure.Type', 'Years')]

> holder2 = data[,c('RepSpExpDiff_nohh','Years')]

> holder2[,Exposure:=RepSpExpDiff_nohh][,Exposure.Type:='Exposure\nto Republicans']

> holder2= holder2[,c('Exposure','Exposure.Type', 'Years')]

> holder = rbind(holder1,holder2)

> rm(holder1,holder2)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3358244   179.4     9897278    528.6    58645897   3132.1
Vcells 10659042313 81322.1 17828138741 136018.0 17827932248 136016.4

> colors = c(`Exposure\nto Democrats` = "#377EB8",`Exposure\nto Republicans` = "#E41A1C")

> g=  ggplot(data=holder, aes(x=Exposure,color=Exposure.Type, linetype=Years))+
+   geom_density()+
+   theme_bw()+
+   facet_grid(rows = vars(Exposure.Type))+
+   scale_linetype_manual(values=c('dotted','dashed', 'solid'))+
+   theme(legend.position='bottom', legend.title=element_blank(), text=element_text(size=18))+
+   scale_color_manual(values=colors) +
+   xlim(-.5,.5)+
+   theme(text=element_text(family='serif', size = 24))+
+   xlab('')+
+   ylab('Density')+
+   guides(color='none')

> library(modelsummary)

> library(gt)

> library(tidyr)

> names(data) = c( 'Exposure\nto Democrats', 'Exposure\nto Republicans', 'Years')

> ###
> library(dplyr)

> library(tibble)

> library(glue)

> library(scales)

> d = holder[,list(Mean=mean(Exposure,na.rm=T), Median=median(Exposure,na.rm=T),SD=sd(Exposure,na.rm=T)),by=c('Years','Exposure.Type')]%>%
+   as_tibble()%>%
+   pivot_longer(Mean:SD)

> library(dplyr)

> d = d %>% mutate(Exposure.Type=factor(Exposure.Type, levels = c('Exposure\nto Republicans','Exposure\nto Democrats')))

> #colors = c(Dem. = '#377EB8', Rep. = '#E41A1C')
> 
> 
> theme_jake_tile2 <- function(base_size = 10, base_family = "serif",
+                              base_line_size = base_size / 22,
+                              base_rect_size = base_size / 22) {
+   # Starts with theme_bw and remove most parts
+   theme_bw(
+     base_size = base_size,
+     base_family = base_family,
+     base_line_size = base_line_size,
+     base_rect_size = base_rect_size
+   ) %+replace%
+     theme(
+       plot.title = element_text(size = 15),
+       panel.grid.major = element_blank(), 
+       panel.grid.minor = element_blank(),
+       axis.ticks      = element_blank(),
+       legend.background = element_blank(),
+       legend.key        = element_blank(),
+       panel.background  = element_blank(),
+       panel.border      = element_blank(),
+       strip.background  = element_blank(),
+       plot.background   = element_blank(),
+       legend.position   = 'none',
+       # axis.title.x=element_blank()
+       
+       complete = TRUE
+     )
+ }

> d$y=1

> d$value=round(d$value, digits = 2)

> d$value=ifelse(d$value==0,0,d$value)

> t=ggplot(d,aes(y=Exposure.Type, x = name,fill=Exposure.Type,  label = sprintf("%0.2f", value)))+
+   geom_tile(alpha=.75)+
+   scale_fill_manual(values=colors)+
+   geom_text( size = 5, alpha =1, color='black') +
+   scale_x_discrete(position = "top") +
+   scale_y_discrete(position = "right") +
+   geom_vline(xintercept = 1:8 - 0.5, colour = "white", size = 1) +
+   geom_hline(yintercept = 1:8 - 0.5, colour = "white", size = 1) +
+   ylab("") +  
+   xlab('') +
+   coord_equal() +
+   theme_jake_tile2()+
+   theme(text=element_text(size=24, family='serif'), legend.title=element_blank(),strip.placement='outside', axis.text.y=element_blank(),legend.position='bottom')+
+   facet_grid(cols = vars(Years))#+

> library(cowplot)

> ggsave(filename = 'figures/FigS13.png', width = 11, height=11,units = 'in',
+        plot = plot_grid(g,t,ncol=1,align='v'))

> toc()
1806.243 sec elapsed

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3355771   179.3     9897278    528.6    58645897   3132.1
Vcells 10451169949 79736.1 17828138741 136018.0 17827932248 136016.4

> # Figure S14
> # If Figure 4 has not yet been created, will need to run the following commented out analysis scripts to create SD output
> tic() # t .... [TRUNCATED] 

> #source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)
> #source('EST-current-results-2012-2016.R', echo = T,  max.deparse. .... [TRUNCATED] 

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3357281   179.3     9897278    528.6    58645897   3132.1
Vcells 10451175916 79736.2 17828138741 136018.0 17827932248 136016.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread(paste0('panel-analysis-2008-2012.csv.gz'),select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff', paste0(c('DemSpExp_nohh', 'RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year2')))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_alt_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_alt_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_alt_year2-hh.r.adj_year1]

> data[,DemSpExpDiff_nohh_alt := DemSpExp_nohh_alt_year2-DemSpExp_nohh_year1]

> data[,RepSpExpDiff_nohh_alt := RepSpExp_nohh_alt_year2-RepSpExp_nohh_year1]

> data = data[!is.na(DemSpExpDiff_nohh_alt)|!is.na(RepSpExpDiff_nohh_alt)]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3364202   179.7     9897278    528.6    58645897   3132.1
Vcells 11006966856 83976.5 17828138741 136018.0 17827932248 136016.4

> # construct grouping variables 
> # we can drop fows where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> #   ###
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2008-2012-alt.Rdata'))

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger   (Mb)    max used     (Mb)
Ncells     3355858   179.3    13798476    737    58645897   3132.1
Vcells 10553001248 80513.1 17828138741 136018 17827932248 136016.4

> source('EST-current-results-2012-2016-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3357406   179.4    11038781    589.6    58645897   3132.1
Vcells 10552989007 80513.0 17828138741 136018.0 17827932248 136016.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff',
+                                                                paste0(c('DemSpExp_nohh','RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year1'),
+                                                                paste0(c('DemSpExp_nohh', 'RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year2')
+                                                                ))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_alt_year2-hh.n.adj_alt_year1]

> data[,hh.d.diff:=hh.d.adj_alt_year2-hh.d.adj_alt_year1]

> data[,hh.r.diff:=hh.r.adj_alt_year2-hh.r.adj_alt_year1]

> data[,DemSpExpDiff_nohh_alt := DemSpExp_nohh_alt_year2-DemSpExp_nohh_alt_year1]

> data[,RepSpExpDiff_nohh_alt := RepSpExp_nohh_alt_year2-RepSpExp_nohh_alt_year1]

> data = data[!is.na(DemSpExpDiff_nohh_alt)|!is.na(RepSpExpDiff_nohh_alt)]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3364844   179.8    11038781    589.6    58645897   3132.1
Vcells 11358468305 86658.3 17828138741 136018.0 17827932248 136016.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2012-2016-alt.Rdata'))

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3355860   179.3    18483788    987.2    58645897   3132.1
Vcells 10679989409 81481.9 17828138741 136018.0 17827932248 136016.4

> source('EST-current-results-2016-2020-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3357426   179.4    14787031    789.8    58645897   3132.1
Vcells 10679991988 81481.9 17828138741 136018.0 17827932248 136016.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2016-2020.csv.gz',select = c('DemDiff','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'Married_year1','State', 'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                             'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                             'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff',
+              paste0(c('DemSpExp_nohh','RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year1'),
+              paste0(c('DemSpExp_nohh', 'RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year2')
+              ))
|--------------------------------------------------|
|==================================================|

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_alt_year2-hh.n.adj_alt_year1]

> data[,hh.d.diff:=hh.d.adj_alt_year2-hh.d.adj_alt_year1]

> data[,hh.r.diff:=hh.r.adj_alt_year2-hh.r.adj_alt_year1]

> data[,DemSpExpDiff_nohh_alt := DemSpExp_nohh_alt_year2-DemSpExp_nohh_alt_year1]

> data[,RepSpExpDiff_nohh_alt := RepSpExp_nohh_alt_year2-RepSpExp_nohh_alt_year1]

> data = data[!is.na(DemSpExpDiff_nohh_alt)|!is.na(RepSpExpDiff_nohh_alt)]

> # split data
> dems = data[Party_year1=='Democrat']

> reps = data[Party_year1=='Republican']

> oths = data[Party_year1=='Other']

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3364268   179.7    11829625    631.8    58645897   3132.1
Vcells 11678197970 89097.6 17828138741 136018.0 17827932248 136016.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) & !is.na(Married_year1) & !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> #   ###
> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/current-results-2016-2020-alt.Rdata'))

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3355865   179.3    19795704   1057.3    58645897   3132.1
Vcells 10821840966 82564.1 17828138741 136018.0 17827932248 136016.4

> source('EST-future-results-2008-2012-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3357487   179.4    15836564    845.8    58645897   3132.1
Vcells 10821841738 82564.2 17828138741 136018.0 17827932248 136016.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread(paste0('panel-analysis-2008-2012.csv.gz'),select = c('voterbase_id','DemDiff','Party_2016','RepDiff','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                            'ZipCode','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                            'State', 'WhiteBlockGroupDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff'  , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff'  , 'HouseValueBlockGroupDiff',
+              paste0(c('DemSpExp_nohh', 'RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year2')))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2012:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2012:='Republican']

> data[is.na(Party_2012), Party_2012:='Other']

> data[Party_2016=='',Party_2016:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_alt_year2-hh.n.adj_year1]

> data[,hh.d.diff:=hh.d.adj_alt_year2-hh.d.adj_year1]

> data[,hh.r.diff:=hh.r.adj_alt_year2-hh.r.adj_year1]

> data[,DemSpExpDiff_nohh_alt := DemSpExp_nohh_alt_year2-DemSpExp_nohh_year1]

> data[,RepSpExpDiff_nohh_alt := RepSpExp_nohh_alt_year2-RepSpExp_nohh_year1]

> data = data[!is.na(DemSpExpDiff_nohh_alt)|!is.na(RepSpExpDiff_nohh_alt)]

> data[,DemDiff2:=as.numeric(Party_2016=='Democrat')-as.numeric(Party_2012=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2016=='Republican')-as.numeric(Party_2012=='Republican')]

> # split data
> dems = data[Party_year1=='Democrat' & Party_2012=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2012=='Republican']

> oths = data[Party_year1=='Other' & Party_2012=='Other']

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3364475   179.7    12669252    676.7    58645897   3132.1
Vcells 11422330068 87145.5 17828138741 136018.0 17827932248 136016.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_year1,hh.d.adj_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_year1,hh.r.adj_year1, sep ='_')]

> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2008-2012-alt.Rdata'))

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3355876   179.3    14665378    783.3    58645897   3132.1
Vcells 10927817014 83372.7 17828138741 136018.0 17827932248 136016.4

> source('EST-future-results-2012-2016-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3357608   179.4    11732303    626.6    58645897   3132.1
Vcells 10927810441 83372.6 17828138741 136018.0 17827932248 136016.4

> ## 06 - ESTIMATE TSMART PID MODELS
> 
> 
> ## LOAD PACKAGES
> library(data.table)

> library(lfe)

> library(stringr)

> data = fread('panel-analysis-2012-2016.csv.gz',select = c('voterbase_id_year1','voterbase_id_year2','DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
+                                                                             'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
+                                                                             'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
+                                                                             'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
+                                                                            'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
+                                                                            'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff',
+              paste0(c('DemSpExp_nohh','RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year1'),
+              paste0(c('DemSpExp_nohh', 'RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year2')))
|--------------------------------------------------|
|==================================================|

> data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']

> data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']

> data[is.na(Party_2016), Party_2016:='Other']

> data[Party_2020=='',Party_2020:=NA]

> data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']

> data[countyfips=='',countyfips:=NA]

> data[ZipCode=='',ZipCode:=NA]

> data[Gender=='',Gender:=NA]

> data[,hh.n.diff:=hh.n.adj_alt_year2-hh.n.adj_alt_year1]

> data[,hh.d.diff:=hh.d.adj_alt_year2-hh.d.adj_alt_year1]

> data[,hh.r.diff:=hh.r.adj_alt_year2-hh.r.adj_alt_year1]

> data[,DemSpExpDiff_nohh_alt := DemSpExp_nohh_alt_year2-DemSpExp_nohh_alt_year1]

> data[,RepSpExpDiff_nohh_alt := RepSpExp_nohh_alt_year2-RepSpExp_nohh_alt_year1]

> data = data[!is.na(DemSpExpDiff_nohh_alt)|!is.na(RepSpExpDiff_nohh_alt)]

> data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]

> data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]

> # split data
> 
> dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']

> reps = data[Party_year1=='Republican' & Party_2016=='Republican']

> oths = data[Party_year1=='Other' & Party_2016=='Other']

> rm(data)

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3364434   179.7     9385843    501.3    58645897   3132.1
Vcells 11787095702 89928.5 17828138741 136018.0 17827932248 136016.4

> # construct grouping variables 
> # we can drop rows where any of these are missing
> 
> 
> 
> dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]

> reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]

> oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

> dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # spatial seg treatment
> # democrat exposure
> dems[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> # republican exposure
> dems[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> reps[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> oths[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

> #####
> # make groups
> # spatial seg treatment
> dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

> dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]

> # estimate models
> 
> # spatial seg treatment
> ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
+                                    HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
+                                    DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)

> save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
+      ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
+      
+      
+      file = paste0('results/future-results-2012-2016-alt.Rdata'))

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3355858   179.3    18922114   1010.6    58645897   3132.1
Vcells 11058903925 84372.8 17828138741 136018.0 17827932248 136016.4

> source('FIG-current-results-dedup.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3356746   179.3    15137692    808.5    58645897   3132.1
Vcells 11058900198 84372.8 17828138741 136018.0 17827932248 136016.4

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c('current-results-2008-2012.Rdata',
+                                         'current-results-2012-2016.Rdata',
+                                         'current-results-2016-2020.Rdata',
+                                         'current-results-2008-2012-alt.Rdata',
+                                         'current-results-2012-2016-alt.Rdata',
+                                         'current-results-2016-2020-alt.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+ l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
+                     grepl('2012-2016',file)~ '2012-2016',
+                     grepl('2016-2020',file)~ '2016-2020')]  
+ l[,file:=file]
+ l[,Sample:=ifelse(grepl('-alt',file),'Alternative', 'Main')]
+ l[,Year1:=substr(Years,1,4)]
+ 
+ return(l)
+ }))

> results[,Sample:=factor(Sample,levels=c('Main','Alternative'))]

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh','DemSpExpDiff_nohh_alt','RepSpExpDiff_nohh_alt')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results, aes(y = Estimate, x = Subset, color = Subset, shape = Sample))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original party')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'), strip.text.y = element_text(size=12,angle=0),legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/FigS14a.png',dpi=300, width = 10, height = 5, units = 'in')

> toc()
5238.169 sec elapsed

> source('FIG-future-results-dedup.R', echo = T,  max.deparse.length=10000)

> ##
> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3345086   178.7    18725127   1000.1    58645897   3132.1
Vcells 10643176703 81201.0 17828138741 136018.0 17827932248 136016.4

> library(data.table)

> library(ggplot2)

> library(dplyr)

> library(stringr)

> files = paste0('results/',c('future-results-2008-2012.Rdata',
+                                         'future-results-2012-2016.Rdata',
+                                         
+                                         'future-results-2008-2012-alt.Rdata',
+                                         'future-results-2012-2016-alt.Rdata'))

> results = rbindlist(lapply(files, FUN=function(file){
+ 
+   
+   load(file)
+   
+   models = ls()[grepl('Model',ls())]
+   
+   l = rbindlist(lapply(models, FUN=function(x){
+    # print(x)
+     m = get(x)
+     out = as.data.table(m$coefficient)
+     out[,Covariate:=rownames(m$coefficient)]
+     out[,Model:=x]
+     out[,N:=m$N]
+     out[,`R-Squared`:=m$r.squared]
+     out[,`Adjusted R-Squared`:=m$adj.r.squared]
+     out[,Sigma:=m$sigma]
+     out[,FStat:=m$fstat]
+     if('Cluster s.e.'%in%names(out)){
+       out[,SE:=`Cluster s.e.`]
+       out[,SE.type:='Cluster']
+       out[,`Cluster s.e.`:=NULL]
+     } else {
+       out[,SE:=`Robust s.e`]
+       out[,SE.type:='Robust']
+       out[,`Robust s.e`:=NULL]
+     }
+ 
+ 
+ return(out)
+ 
+   }))
+   
+ l[,Years:=case_when(grepl('2008-2012',file)~ 'EV: 2008-2012 -- DV: 2012-2016',
+                     grepl('2012-2016',file)~ 'EV: 2012-2016 -- DV: 2016-2020')]  
+ l[,file:=file]
+ l[,Sample:=ifelse(grepl('-alt',file),'Alternative', 'Main')]
+ l[,Year1:=substr(Years,1,4)]
+ 
+ return(l)
+ }))

> results[,Sample:=factor(Sample,levels=c('Main','Alternative'))]

> results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh','DemSpExpDiff_nohh_alt','RepSpExpDiff_nohh_alt')]

> results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']

> results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

> results[grepl('Dems',Model),Subset:='Democrats']

> results[grepl('Reps',Model),Subset:='Republicans']

> results[grepl('Oths',Model),Subset:='Non-partisans']

> colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

> g1 = ggplot(results, aes(y = Estimate, x = Subset, color = Subset, shape = Sample))+
+   geom_point(size = 5, position = position_dodge(width = 0.75))+
+   geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
+   geom_hline(yintercept = 0, linetype = 'dashed')+
+   coord_flip()+
+   theme_bw()+
+   scale_color_manual(values = colors)+
+   ylab('Coefficient on change in partisan exposure')+
+   xlab('Original partisan registration')+
+   guides(color = 'none')+
+   theme(text = element_text(size = 15, family = 'serif'), strip.text.y = element_text(size=12,angle=0),legend.title=element_blank(),legend.position = 'bottom')+
+   facet_grid(Exposure.Type~Years)

> ggsave(plot = g1,filename = 'figures/FigS14b.png',dpi=300, width = 10, height = 5, units = 'in')

> toc()
138336.437 sec elapsed

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3344996   178.7    14412898    769.8    58645897   3132.1
Vcells 10643173864 81201.0 17828138741 136018.0 17827932248 136016.4

> # Table S20
> tic() # time scripts for each figure

> source('EST-survey-analysis-weights-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list = ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3346531   178.8    11530319    615.8    58645897   3132.1
Vcells 10643174028 81201.0 17828138741 136018.0 17827932248 136016.4

> library(data.table)

> library(dplyr)

> library(stringr)

> library(tidyr)

> library(gender)

> library(lfe)

> surv=fread('survey-analysis.csv.gz')

> surv[countyfips=='',countyfips:=NA]

> surv[ZipCode=='',ZipCode:=NA]

> surv[,Race:=factor(Race,levels=c('Other','Asian','Black','Hispanic','White'))]

> # Perceptions of neighborhoods
> 
> 
> m = felm(NeighborDems~ DemSpExp_nohh_alt +hh.d.adj_alt+hh.n.adj_alt  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w1DemSpExp = summary(m,robust=T)

> w1DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(NeighborDems~ RepSpExp_nohh_alt  +hh.r.adj_alt+hh.n.adj_alt + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w1RepSpExp = summary(m,robust=T)

> w1RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> #### Contact with neighbor dems
> 
> m = felm(ContactDemNeighbors~ DemSpExp_nohh_alt +hh.d.adj_alt+hh.n.adj_alt  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w2DemSpExp = summary(m,robust=T)

> w2DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(ContactRepNeighbors~ RepSpExp_nohh_alt +hh.r.adj_alt+hh.n.adj_alt + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w2RepSpExp = summary(m,robust=T)

> w2RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> # Comfort
> m = felm(Comfort~(DemSpExp_nohh_alt + hh.d.adj_alt+hh.n.adj_alt+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Democrat |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w5DemSpExp = summary(m,robust=T)

> w5DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Comfort~(RepSpExp_nohh_alt  + hh.r.adj_alt+hh.n.adj_alt+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Republican |ZipCode|0|countyfips,data=surv, weights =  surv$w)

> w5RepSpExp = summary(m,robust=T)

> w5RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

> # Dem models
> dems = surv[Party_year1=='Democrat']

> reps = surv[Party_year1=='Republican']

> oths = surv[Party_year1=='Other']

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems, weights =  dems$w)

> w6DemSpExpDems = summary(m,robust=T)

> w6DemSpExpDems$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps, weights =  reps$w)

> w6DemSpExpReps = summary(m,robust=T)

> w6DemSpExpReps$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths, weights =  oths$w)

> w6DemSpExpOths = summary(m,robust=T)

> w6DemSpExpOths$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems, weights =  dems$w)

> w6RepSpExpDems = summary(m,robust=T)

> w6RepSpExpDems$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps, weights =  reps$w)

> w6RepSpExpReps = summary(m,robust=T)

> w6RepSpExpReps$mean.outcome=weighted.mean(m$response, w=m$weights)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths, weights =  oths$w)

> w6RepSpExpOths = summary(m,robust=T)

> w6RepSpExpOths$mean.outcome=weighted.mean(m$response, w=m$weights)

> ##
> rm(surv,w,data,dems,reps,oths,m,t)

> save.image(file='results/survey-results-weights-alt.Rdata')

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3345580   178.7     9224256    492.7    58645897   3132.1
Vcells 10643296974 81202.0 17828138741 136018.0 17827932248 136016.4

> source('EST-survey-analysis-no-weights-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list = ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3346458   178.8     9224256    492.7    58645897   3132.1
Vcells 10643252485 81201.6 17828138741 136018.0 17827932248 136016.4

> library(data.table)

> library(dplyr)

> library(stringr)

> library(tidyr)

> library(gender)

> library(lfe)

> surv=fread('survey-analysis.csv.gz')

> surv[countyfips=='',countyfips:=NA]

> surv[ZipCode=='',ZipCode:=NA]

> surv[,Race:=factor(Race,levels=c('Other','Asian','Black','Hispanic','White'))]

> # Perceptions of neighborhoods
> 
> 
> m = felm(NeighborDems~ DemSpExp_nohh_alt +hh.d.adj_alt+hh.n.adj_alt  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m1DemSpExp = summary(m,robust=T)

> m1DemSpExp$mean.outcome=mean(m$response)

> m = felm(NeighborDems~ RepSpExp_nohh_alt  +hh.r.adj_alt+hh.n.adj_alt + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m1RepSpExp = summary(m,robust=T)

> m1RepSpExp$mean.outcome=mean(m$response)

> #### Contact with neighbor dems
> 
> m = felm(ContactDemNeighbors~ DemSpExp_nohh_alt +hh.d.adj_alt+hh.n.adj_alt  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m2DemSpExp = summary(m,robust=T)

> m2DemSpExp$mean.outcome=mean(m$response)

> m = felm(ContactRepNeighbors~ RepSpExp_nohh_alt +hh.r.adj_alt+hh.n.adj_alt + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)

> m2RepSpExp = summary(m,robust=T)

> m2RepSpExp$mean.outcome=mean(m$response)

> # Comfort
> m = felm(Comfort~(DemSpExp_nohh_alt + hh.d.adj_alt+hh.n.adj_alt+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Democrat |ZipCode|0|countyfips,data=surv)

> m5DemSpExp = summary(m,robust=T)

> m5DemSpExp$mean.outcome=mean(m$response)

> m = felm(Comfort~(RepSpExp_nohh_alt  + hh.r.adj_alt+hh.n.adj_alt+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Republican |ZipCode|0|countyfips,data=surv)

> m5RepSpExp = summary(m,robust=T)

> m5RepSpExp$mean.outcome=mean(m$response)

> # Dem models
> dems = surv[Party_year1=='Democrat']

> reps = surv[Party_year1=='Republican']

> oths = surv[Party_year1=='Other']

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems)

> m6DemSpExpDems = summary(m,robust=T)

> m6DemSpExpDems$mean.outcome=(m$response)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps)

> m6DemSpExpReps = summary(m,robust=T)

> m6DemSpExpReps$mean.outcome=(m$response)

> m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths)

> m6DemSpExpOths = summary(m,robust=T)

> m6DemSpExpOths$mean.outcome=(m$response)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems)

> m6RepSpExpDems = summary(m,robust=T)

> m6RepSpExpDems$mean.outcome=(m$response)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps)

> m6RepSpExpReps = summary(m,robust=T)

> m6RepSpExpReps$mean.outcome=(m$response)

> m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths)

> m6RepSpExpOths = summary(m,robust=T)

> m6RepSpExpOths$mean.outcome=(m$response)

> ##
> rm(surv,w,data,dems,reps,oths,m,t)

> save.image(file='results/survey-results-no-weights-alt.Rdata')

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3345580   178.7     9224256    492.7    58645897   3132.1
Vcells 10643376692 81202.6 17828138741 136018.0 17827932248 136016.4

> source('TAB-survey-results-dedup.R', echo = T,  max.deparse.length=10000)

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3345565   178.7     9224256    492.7    58645897   3132.1
Vcells 10643324610 81202.2 17828138741 136018.0 17827932248 136016.4

> ## SURVEY RESULT TABLES
> library(modelsummary)

> library(dplyr)

> library(tidyr)

> library(stringr)

> library(tibble)

> library(readr)

> library(gt)

> library(data.table)

> library(gt)

> #gc()
> 
> load('results/survey-results-no-weights-alt.Rdata')

> load('results/survey-results-weights-alt.Rdata')

> source('felm-summary-table.R')

> vars = c('Dem Exp', 'HH Dem',  'Dem Exp * Dem','HH Dem * Dem', 'Rep Exp', 'HH Rep' ,'Rep Exp * Rep', 'HH Rep * Rep')

> names(vars)= c('DemSpExp_nohh_alt', 'hh.d.adj','DemSpExp_nohh_alt:Democrat','hh.d.adj:Democrat','RepSpExp_nohh_alt','hh.r.adj', 'RepSpExp_nohh_alt:Republican','hh.r.adj:Republican')

> l = list(w1DemSpExp, m1DemSpExp,
+          w1RepSpExp, m1RepSpExp,
+          w2DemSpExp, m2DemSpExp,
+          w2RepSpExp, m2RepSpExp,
+          w5DemSpExp, m5DemSpExp,
+          w5RepSpExp, m5RepSpExp)

> names(l) = c('(1)', '(2)','(3)', '(4)', '(5)', '(6)', '(7)', '(8)','(9)', '(10)', '(11)', '(12)')

> tab1 = felm.summary.table(summaries = l,
+                           coef_map = vars, output ='gt',
+                           model.names=names(l), fmt =3,
+                           add_rows = data.frame(v1 = 'Weighted',
+                                                 v2 = 'Yes',
+                                                 v3 = 'No',
+                                                 v4 = 'Yes',
+                                                 v5 = 'No',
+                                                 v6 = 'Yes',
+                                                 v7 = 'No',
+                                                 v8 = 'Yes',
+                                                 v9 = 'No',
+                                                 v10 = 'Yes',
+                                                 v11 = 'No',
+                                                 v12 = 'Yes',
+                                                 v13 = 'No'))

> tab1 %>%
+   
+   # column labels
+   tab_spanner(label = 'Neighbors: Democrats or Republicans', columns = 2:5) %>%
+   tab_spanner(label = 'Contact: Democrats', columns = 6:7) %>%
+   tab_spanner(label = 'Contact: Republicans', columns = 8:9) %>%
+   tab_spanner(label = 'Comfort: Neighbors know Party', columns = 10:13) %>%
+   
+   
+   as_latex()%>%
+   as.character %>%
+   str_replace_all('longtable','tabular')%>%
+   write_file('tables/TabS20.tex')

> toc()
17.687 sec elapsed

> rm(list=ls())

> gc()
              used    (Mb)  gc trigger     (Mb)    max used     (Mb)
Ncells     3346474   178.8     9224256    492.7    58645897   3132.1
Vcells 10643354861 81202.4 17828138741 136018.0 17827932248 136016.4

> toc()

> print(Sys.time())
[1] "2024-02-13 14:35:31 EST"

> ####################
> # Restore output to console
> sink() 
